]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/test/test_magic_folder.py
Debugging WIP.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_magic_folder.py
index c2931b0755519e8b166ac33f0540b99710a2ac5d..cacb18044b90165150deb3bb05c1700b1443fe09 100644 (file)
@@ -153,7 +153,8 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             downloaded_d = self.magicfolder.downloader.set_hook('processed', ignore_count=1)
             uploaded_d = self.magicfolder.uploader.set_hook('processed', ignore_count=1)
             self.mkdir_nonascii(small_tree_dir)
-            fileutil.write(abspath_expanduser_unicode(u"what", base=small_tree_dir), "say when")
+            what_path = abspath_expanduser_unicode(u"what", base=small_tree_dir)
+            fileutil.write(what_path, "say when")
             os.rename(small_tree_dir, new_small_tree_dir)
             self.notify(to_filepath(new_small_tree_dir), self.inotify.IN_MOVED_TO)
 
@@ -169,8 +170,9 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             print "_check_moved_tree_is_watched"
             downloaded_d = self.magicfolder.downloader.set_hook('processed', ignore_count=1)
             uploaded_d = self.magicfolder.uploader.set_hook('processed')
-            fileutil.write(abspath_expanduser_unicode(u"another", base=new_small_tree_dir), "file")
-            self.notify(to_filepath(abspath_expanduser_unicode(u"another", base=new_small_tree_dir)), self.inotify.IN_CLOSE_WRITE)
+            another_path = abspath_expanduser_unicode(u"another", base=new_small_tree_dir)
+            fileutil.write(another_path, "file")
+            self.notify(to_filepath(another_path), self.inotify.IN_CLOSE_WRITE)
 
             return DeferredListShouldSucceed([downloaded_d, uploaded_d])
         d.addCallback(_check_moved_tree_is_watched)
@@ -255,8 +257,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             up_proc = self.magicfolder.uploader.set_hook('processed')
             # down_proc = self.magicfolder.downloader.set_hook('processed')
             path = os.path.join(self.local_dir, u'foo')
-            with open(path, 'w') as f:
-                f.write('foo\n')
+            fileutil.write(path, 'foo\n')
             self.notify(to_filepath(path), self.inotify.IN_CLOSE_WRITE)
             yield up_proc
             self.assertTrue(os.path.exists(path))
@@ -290,8 +291,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             up_proc = self.magicfolder.uploader.set_hook('processed')
             # down_proc = self.magicfolder.downloader.set_hook('processed')
             path = os.path.join(self.local_dir, u'foo')
-            with open(path, 'w') as f:
-                f.write('foo\n')
+            fileutil.write(path, 'foo\n')
             self.notify(to_filepath(path), self.inotify.IN_CLOSE_WRITE)
             yield up_proc
             self.assertTrue(os.path.exists(path))
@@ -311,8 +311,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             # restore the file, with different contents
             up_proc = self.magicfolder.uploader.set_hook('processed')
             path = os.path.join(self.local_dir, u'foo')
-            with open(path, 'w') as f:
-                f.write('bar\n')
+            fileutil.write(path, 'bar\n')
             self.notify(to_filepath(path), self.inotify.IN_CLOSE_WRITE)
             yield up_proc
 
@@ -340,8 +339,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             alice_proc = self.alice_magicfolder.uploader.set_hook('processed')
             bob_proc = self.bob_magicfolder.downloader.set_hook('processed')
 
-            with open(alice_fname, 'wb') as f:
-                f.write('contents0\n')
+            fileutil.write(alice_fname, 'contents0\n')
             self.notify(to_filepath(alice_fname), self.inotify.IN_CLOSE_WRITE, magic=self.alice_magicfolder)
 
             alice_clock.advance(0)
@@ -388,8 +386,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             # now alice restores it (alice should upload, bob download)
             alice_proc = self.alice_magicfolder.uploader.set_hook('processed')
             bob_proc = self.bob_magicfolder.downloader.set_hook('processed')
-            with open(alice_fname, 'wb') as f:
-                f.write('new contents\n')
+            fileutil.write(alice_fname, 'new contents\n')
             self.notify(to_filepath(alice_fname), self.inotify.IN_CLOSE_WRITE, magic=self.alice_magicfolder)
 
             alice_clock.advance(0)
@@ -430,8 +427,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             alice_proc = self.alice_magicfolder.uploader.set_hook('processed')
             bob_proc = self.bob_magicfolder.downloader.set_hook('processed')
 
-            with open(alice_fname, 'wb') as f:
-                f.write('contents0\n')
+            fileutil.write(alice_fname, 'contents0\n')
             self.notify(to_filepath(alice_fname), self.inotify.IN_CLOSE_WRITE, magic=self.alice_magicfolder)
 
             alice_clock.advance(0)
@@ -457,8 +453,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             # now bob updates it (bob should upload, alice download)
             bob_proc = self.bob_magicfolder.uploader.set_hook('processed')
             alice_proc = self.alice_magicfolder.downloader.set_hook('processed')
-            with open(bob_fname, 'wb') as f:
-                f.write('bob wuz here\n')
+            fileutil.write(bob_fname, 'bob wuz here\n')
             self.notify(to_filepath(bob_fname), self.inotify.IN_CLOSE_WRITE, magic=self.bob_magicfolder)
 
             bob_clock.advance(0)
@@ -497,8 +492,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             alice_proc = self.alice_magicfolder.uploader.set_hook('processed')
             bob_proc = self.bob_magicfolder.downloader.set_hook('processed')
 
-            with open(alice_fname, 'wb') as f:
-                f.write('contents0\n')
+            fileutil.write(alice_fname, 'contents0\n')
             self.notify(to_filepath(alice_fname), self.inotify.IN_CLOSE_WRITE, magic=self.alice_magicfolder)
 
             alice_clock.advance(0)
@@ -543,8 +537,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             # now alice restores the file (with new contents)
             alice_proc = self.alice_magicfolder.uploader.set_hook('processed')
             bob_proc = self.bob_magicfolder.downloader.set_hook('processed')
-            with open(alice_fname, 'wb') as f:
-                f.write('alice wuz here\n')
+            fileutil.write(alice_fname, 'alice wuz here\n')
             self.notify(to_filepath(alice_fname), self.inotify.IN_CLOSE_WRITE, magic=self.alice_magicfolder)
 
             alice_clock.advance(0)
@@ -605,6 +598,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
     def _check_file(self, name_u, data, temporary=False, directory=False):
         precondition(not (temporary and directory), temporary=temporary, directory=directory)
 
+        print "%r._check_file(%r, %r, temporary=%r, directory=%r)" % (self, name_u, data, temporary, directory)
         previously_uploaded = self._get_count('uploader.objects_succeeded')
         previously_disappeared = self._get_count('uploader.objects_disappeared')
 
@@ -628,10 +622,9 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
                 f.close()
             if temporary and sys.platform == "win32":
                 os.unlink(path_u)
-                self.notify(path, self.inotify.IN_DELETE)
+                self.notify(path, self.inotify.IN_DELETE, flush=False)
             event_mask = self.inotify.IN_CLOSE_WRITE
 
-        fileutil.flush_volume(path_u)
         self.notify(path, event_mask)
         encoded_name_u = magicpath.path2magic(name_u)
 
@@ -640,9 +633,18 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
             d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('uploader.objects_disappeared'),
                                                                  previously_disappeared + 1))
         else:
+            def _here(res, n):
+                print "here %r %r" % (n, res)
+                return res
+            d.addBoth(_here, 1)
+            d.addCallback(lambda ign: self.upload_dirnode.list())
+            d.addBoth(_here, 1.5)
             d.addCallback(lambda ign: self.upload_dirnode.get(encoded_name_u))
+            d.addBoth(_here, 2)
             d.addCallback(download_to_data)
+            d.addBoth(_here, 3)
             d.addCallback(lambda actual_data: self.failUnlessReallyEqual(actual_data, data))
+            d.addBoth(_here, 4)
             d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('uploader.objects_succeeded'),
                                                                  previously_uploaded + 1))
 
@@ -740,7 +742,7 @@ class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqual
         def notify_bob_moved(ign):
             d0 = self.bob_magicfolder.uploader.set_hook('processed')
             p = abspath_expanduser_unicode(u"file1", base=self.bob_magicfolder.uploader._local_path_u)
-            self.notify(to_filepath(p), self.inotify.IN_MOVED_FROM, magic=self.bob_magicfolder)
+            self.notify(to_filepath(p), self.inotify.IN_MOVED_FROM, magic=self.bob_magicfolder, flush=False)
             self.notify(to_filepath(p + u'.backup'), self.inotify.IN_MOVED_TO, magic=self.bob_magicfolder)
             bob_clock.advance(0)
             return d0
@@ -876,10 +878,11 @@ class MockTest(MagicFolderTestMixin, unittest.TestCase):
         self.inotify = fake_inotify
         self.patch(magic_folder, 'get_inotify_module', lambda: self.inotify)
 
-    def notify(self, path, mask, magic=None):
+    def notify(self, path, mask, magic=None, flush=True):
         if magic is None:
             magic = self.magicfolder
         magic.uploader._notifier.event(path, mask)
+        # no flush for the mock test.
 
     def test_errors(self):
         self.set_up_grid()
@@ -909,7 +912,7 @@ class MockTest(MagicFolderTestMixin, unittest.TestCase):
             self.shouldFail(AssertionError, 'readonly upload.dircap', 'is not a writecap to a directory',
                             MagicFolder, client, readonly_dircap, '', errors_dir, magicfolderdb,)
             self.shouldFail(AssertionError, 'collective dircap',
-                            "The URI in 'private/collective_dircap' is not a readonly cap to a directory.",
+                            "The URI in '%s' is not a readonly cap to a directory." % os.path.join('private', 'collective_dircap'),
                             MagicFolder, client, upload_dircap, upload_dircap, errors_dir, magicfolderdb)
 
             def _not_implemented():
@@ -966,9 +969,11 @@ class RealTest(MagicFolderTestMixin, unittest.TestCase):
         MagicFolderTestMixin.setUp(self)
         self.inotify = magic_folder.get_inotify_module()
 
-    def notify(self, path, mask, **kw):
+    def notify(self, path, mask, magic=None, flush=True):
         # Writing to the filesystem causes the notification.
-        pass
+        # However, flushing filesystem buffers may be necessary on Windows.
+        if flush:
+            fileutil.flush_volume(path.path)
 
 try:
     magic_folder.get_inotify_module()