11# -*- coding: utf-8 -*-
2+ import time
23import unittest
34
45import cherrypy
56from ws4py .server .cherrypyserver import WebSocketPlugin , WebSocketTool
67from ws4py .websocket import EchoWebSocket
7- from ws4py .compat import py3k
88
99class FakeSocket (object ):
1010 def settimeout (self , timeout ):
@@ -28,6 +28,12 @@ def fileno(self):
2828 def recv (self , bufsize = 0 ):
2929 pass
3030
31+ def getsockname (self ):
32+ return '127.0.0.1' , 0
33+
34+ def getpeername (self ):
35+ return '127.0.0.1' , 8091
36+
3137class App (object ):
3238 @cherrypy .expose
3339 def ws (self ):
@@ -60,24 +66,20 @@ def tearDown(self):
6066 teardown_engine ()
6167
6268 def test_plugin (self ):
63- self .assertEquals (len (cherrypy .engine .websocket .pool ), 0 )
69+ manager = cherrypy .engine .websocket .manager
70+ self .assertEquals (len (manager ), 0 )
6471
6572 s = FakeSocket ()
6673 h = EchoWebSocket (s , [], [])
6774 cherrypy .engine .publish ('handle-websocket' , h , ('127.0.0.1' , 0 ))
68- self .assertEquals (len (cherrypy .engine .websocket .pool ), 1 )
69- if py3k :
70- k = list (cherrypy .engine .websocket .pool .keys ())[0 ]
71- else :
72- k = cherrypy .engine .websocket .pool .keys ()[0 ]
73- self .assertTrue (k is h )
74- self .assertEquals (cherrypy .engine .websocket .pool [k ][1 ], ('127.0.0.1' , 0 ))
75-
76- self .assertEquals (len (cherrypy .engine .websocket .pool ), 1 )
77- h .close () # shutdown server side of the websocket connection
75+ self .assertEquals (len (manager ), 1 )
76+ self .assertIn (h , manager )
77+
78+ h .close () # initiate closing handshake
7879 h .client_terminated = True # we aren't actually connected so pretend the client shutdown
79- cherrypy .engine .publish ('main' )
80- self .assertEquals (len (cherrypy .engine .websocket .pool ), 0 )
80+
81+ # TODO: Implement a fake poller so that works...
82+ self .assertEquals (len (manager ), 0 )
8183
8284if __name__ == '__main__' :
8385 suite = unittest .TestSuite ()
0 commit comments