~nataliabidart/ubuntuone-client/credentials-shutdown

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_action_queue.py

  • Committer: Tarmac
  • Author(s): facundo at com
  • Date: 2011-01-07 12:21:24 UTC
  • mfrom: (782.2.1 unleash-the-queues-1)
  • Revision ID: tarmac-20110107122124-p9k4s3pvjmqc3wz9
Unleash the queues: some AQ small changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#
3
3
# Author: John R. Lenton <john.lenton@canonical.com>
4
4
# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
 
5
# Author: Facundo Batista <facundo@canonical.com>
5
6
#
6
 
# Copyright 2009 Canonical Ltd.
 
7
# Copyright 2009, 2010, 2011 Canonical Ltd.
7
8
#
8
9
# This program is free software: you can redistribute it and/or modify it
9
10
# under the terms of the GNU General Public License version 3, as published
55
56
    CreateShare, DeleteShare, GetPublicFiles, GetDelta, GetDeltaFromScratch,
56
57
    TRANSFER_PROGRESS_THRESHOLD, Unlink, Move, MakeFile, MakeDir, DeltaList,
57
58
    ZipQueue, DeferredMap, UniqueRequestQueue, ThrottlingStorageClient,
 
59
    UploadCompressionCancelled, ClientNoLongerThere,
58
60
)
59
61
from ubuntuone.syncdaemon.event_queue import EventQueue, EVENTS
60
62
from ubuntuone.syncdaemon.marker import MDMarker
97
99
        self.share_id = share_id
98
100
        self.node_id = node_id
99
101
        self.cancelled = False
 
102
        self.log = logging.getLogger('ubuntuone.SyncDaemon')
100
103
 
101
104
    def run(self):
102
105
        """Run that just succeeds."""
1122
1125
    @defer.inlineCallbacks
1123
1126
    def test_zip_calls_compress_in_thread(self):
1124
1127
        """Test that self._compress is called in another thread."""
1125
 
        called = []
1126
 
        def fake_compress(deferred, upload):
1127
 
            """Fake the _compress method."""
1128
 
            self.assertEqual(upload, 'foo')
1129
 
            called.append(True)
1130
 
            deferred.callback(True)
1131
 
 
1132
 
        self.zq._compress = fake_compress
1133
 
        yield self.zq.zip('foo')
1134
 
        self.assertTrue(called)
 
1128
        upload = FakeCommand()
 
1129
        upload.fileobj_factory = lambda: "foo"
 
1130
 
 
1131
        def fake_compress(deferred, _upload, fileobj):
 
1132
            """Fake the _compress method."""
 
1133
            self.assertEqual(upload, _upload)
 
1134
            self.assertEqual(fileobj, "foo")
 
1135
            deferred.callback(True)
 
1136
 
 
1137
        self.zq._compress = fake_compress
 
1138
        yield self.zq.zip(upload)
 
1139
 
 
1140
    @defer.inlineCallbacks
 
1141
    def test_zip_calls_compress_with_file_object(self):
 
1142
        """Test that _compress is called with the result of fileobj factory."""
 
1143
        upload = FakeCommand()
 
1144
        upload.fileobj_factory = lambda: "foo"
 
1145
 
 
1146
        def fake_compress(deferred, upload, fileobj):
 
1147
            """Fake the _compress method."""
 
1148
            deferred.callback(True)
 
1149
 
 
1150
        self.zq._compress = fake_compress
 
1151
        yield self.zq.zip(upload)
 
1152
 
 
1153
    @defer.inlineCallbacks
 
1154
    def test_fileobj_factory_error_is_logged(self):
 
1155
        """Log the error when fileobj_factory fails."""
 
1156
        def crash():
 
1157
            """Crash!"""
 
1158
            raise ValueError("foo")
 
1159
 
 
1160
        upload = FakeCommand()
 
1161
        upload.fileobj_factory = crash
 
1162
 
 
1163
        # set up the logger
 
1164
        self.handler = MementoHandler()
 
1165
        self.handler.setLevel(logging.DEBUG)
 
1166
        upload.log.addHandler(self.handler)
 
1167
 
 
1168
        try:
 
1169
            yield self.zq.zip(upload)
 
1170
        except UploadCompressionCancelled:
 
1171
            # consume the error
 
1172
            pass
 
1173
 
 
1174
        self.assertTrue(self.handler.check_warning("Unable to build fileobj",
 
1175
                                                   "ValueError", "foo"))
 
1176
 
 
1177
    @defer.inlineCallbacks
 
1178
    def test_fileobj_factory_error_cancels_upload(self):
 
1179
        """Cancel the upload when fileobj_factory fails."""
 
1180
        upload = FakeCommand()
 
1181
 
 
1182
        try:
 
1183
            yield self.zq.zip(upload)
 
1184
        except UploadCompressionCancelled:
 
1185
            # consume the error
 
1186
            pass
 
1187
 
 
1188
        self.assertTrue(upload.cancelled)
 
1189
 
 
1190
    @defer.inlineCallbacks
 
1191
    def test_fileobj_factory_error_raises(self):
 
1192
        """Raise the exception when fileobj_factory fails."""
 
1193
        upload = FakeCommand()
 
1194
        try:
 
1195
            yield self.zq.zip(upload)
 
1196
        except UploadCompressionCancelled:
 
1197
            pass
 
1198
        else:
 
1199
            self.fail("Test should have raised UploadCompressionCancelled")
1135
1200
 
1136
1201
    @defer.inlineCallbacks
1137
1202
    def test_zip_acquire_lock(self):
1138
1203
        """Test that it acquires the lock."""
1139
1204
        called = []
1140
 
        self.zq._compress = lambda deferred, upload: deferred.callback(True)
 
1205
        self.zq._compress = lambda deferred, upl, fobj: deferred.callback(True)
1141
1206
 
1142
1207
        def fake_acquire():
1143
1208
            """Fake the acquire method."""
1146
1211
            return defer.succeed(True)
1147
1212
 
1148
1213
        self.zq.acquire = fake_acquire
1149
 
        yield self.zq.zip('foo')
 
1214
        upload = FakeCommand()
 
1215
        upload.fileobj_factory = lambda: "foo"
 
1216
        yield self.zq.zip(upload)
1150
1217
        self.assertTrue(called)
1151
1218
 
1152
1219
    @defer.inlineCallbacks
1153
1220
    def test_zip_release_lock_ok(self):
1154
1221
        """Test that it releases the lock when all ok."""
1155
1222
        called = []
1156
 
        self.zq._compress = lambda deferred, upload: deferred.callback(True)
 
1223
        self.zq._compress = lambda deferred, upl, fobj: deferred.callback(True)
1157
1224
        self.zq.release = lambda: called.append(True)
1158
1225
 
1159
 
        yield self.zq.zip('foo')
 
1226
        upload = FakeCommand()
 
1227
        upload.fileobj_factory = lambda: "foo"
 
1228
        yield self.zq.zip(upload)
1160
1229
        self.assertTrue(called)
1161
1230
 
1162
1231
    @defer.inlineCallbacks
1164
1233
        """Test that it releases the lock even on error."""
1165
1234
        called = []
1166
1235
        exc = Exception('bad')
1167
 
        self.zq._compress = lambda deferred, upload: deferred.errback(exc)
 
1236
        self.zq._compress = lambda deferred, upl, fobj: deferred.errback(exc)
1168
1237
        self.zq.release = lambda: called.append(True)
 
1238
        upload = FakeCommand()
 
1239
        upload.fileobj_factory = lambda: "foo"
1169
1240
 
1170
1241
        try:
1171
 
            yield self.zq.zip('foo')
 
1242
            yield self.zq.zip(upload)
1172
1243
        except Exception, e:
1173
1244
            # need to silent the exception we're generating in the test
1174
1245
            self.assertEqual(e, exc)
2118
2189
        self.command.end_errback(failure=Failure(err))
2119
2190
        return self.deferred
2120
2191
 
 
2192
    def test_retry_clientnolongerthere(self):
 
2193
        """Check that it retries when ConnectionLost."""
 
2194
        self.command.running = True
 
2195
        self.command.end_errback(failure=Failure(ClientNoLongerThere()))
 
2196
        return self.deferred
 
2197
 
2121
2198
 
2122
2199
class ListVolumesTestCase(ConnectedBaseTestCase):
2123
2200
    """Test for ListVolumes ActionQueueCommand."""
2782
2859
 
2783
2860
    @defer.inlineCallbacks
2784
2861
    def test_compress_failed_pushes_upload_error(self):
 
2862
        """Test that upload error is pushed when compress fails."""
2785
2863
        msg = 'Zip can not be accomplished.'
2786
2864
        error = DefaultException(msg)
2787
2865
        self.action_queue.zip_queue.zip = lambda upload: defer.fail(error)
2892
2970
        u = self.command.uniqueness
2893
2971
        self.assertEqual(u, ('MyUpload', self.share_id, 'a_node_id'))
2894
2972
 
 
2973
    @defer.inlineCallbacks
 
2974
    def test_loses_client_while_in_start(self):
 
2975
        """Handle losing the client while in _start."""
 
2976
        self.action_queue.client = None
 
2977
        try:
 
2978
            yield self.command._run()
 
2979
        except ClientNoLongerThere, e:
 
2980
            self.assertEqual(str(e), 'Detected in _run')
 
2981
        else:
 
2982
            self.fail("Test should have raised ClientNoLongerThere")
 
2983
 
2895
2984
 
2896
2985
class CreateShareTestCase(ConnectedBaseTestCase):
2897
2986
    """Test for CreateShare ActionQueueCommand."""