testing sftp under windows

Robey Pointer robey at lag.net
Mon Nov 14 19:05:52 GMT 2005


On 14 Nov 2005, at 2:33, Alexey Shamrin wrote:

> On 14/11/05, Alexey Shamrin <shamrin at gmail.com> wrote:
>>> I changed the listen port to 0 so it can grab any port it wants, in
>>> case 5555 is in use.
>> Oh, good! I didn't know I can pass 0 as a port number...
>>> Also I didn't like that we had to call get_transport() to make the
>>> test clean up correctly, so I tried having stop() (called from
>>> tearDown()) ensure that the socket is closed.  I think the reason
>>> this doesn't show up on posix is that posix sockets are closed in
>>> their __del__ methods.
>> This didn't help... After invoking a first empty test (that was
>> test_delete), all other tests stopped calling tearDown method of
>> TestCaseWithSFTPServer. I don't know the reason of this behaviour...
>
> Oops... I forgot to mention that I tried this part of a fix (calling
> stop() from tearDown()) without changing port 55555 to 0. Of course if
> I set port to 0 it doesn't matter if socket is properly closed. Though
> it will be nice if it is closed, but not necessary.

Well, crap. :)  I can even reproduce that on posix, probably because  
we're trying to grab the same port again too quickly.  I changed it  
to set REUSEADDR on the socket, and to not bother setting up the  
listening port until a test actually calls get_transport() (no need  
to waste the effort on empty tests) and that seems to work even if  
you force it to use port 5555 each time.

Below is the patch, which is also in my branch now.

robey


=== modified file 'bzrlib/selftest/testsftp.py'
--- bzrlib/selftest/testsftp.py
+++ bzrlib/selftest/testsftp.py
@@ -54,6 +54,7 @@
          threading.Thread.__init__(self)
          self._callback = callback
          self._socket = socket.socket()
+        self._socket.setsockopt(socket.SOL_SOCKET,  
socket.SO_REUSEADDR, 1)
          self._socket.bind(('localhost', 0))
          self._socket.listen(1)
          self.port = self._socket.getsockname()[1]
@@ -67,10 +68,6 @@

      def stop(self):
          self.stop_event.set()
-        try:
-            self._socket.close()
-        except:
-            pass


class TestCaseWithSFTPServer (TestCaseInTempDir):
@@ -96,20 +93,31 @@
          TestCaseInTempDir.setUp(self)
          self._root = self.test_dir
+    def delayed_setup(self):
+        # some tests are just stubs that call setUp and then  
immediately call
+        # tearDwon.  so don't create the port listener until  
get_transport is
+        # called and we know we're in an actual test.
          self._listener = SingleListener(self._run_server)
          self._listener.setDaemon(True)
          self._listener.start()
          self._sftp_url = 'sftp://foo:bar@localhost:%d/' %  
(self._listener.port,
)

      def tearDown(self):
-        self._listener.stop()
+        try:
+            self._listener.stop()
+        except AttributeError:
+            pass
          TestCaseInTempDir.tearDown(self)

class SFTPTransportTest (TestCaseWithSFTPServer, TestTransportMixIn):
      readonly = False
+    setup = True

      def get_transport(self):
+        if self.setup:
+            self.delayed_setup()
+            self.setup = False
          from bzrlib.transport.sftp import SFTPTransport
          url = self._sftp_url
          return SFTPTransport(url)






More information about the bazaar mailing list