def test_part(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c1 = yield self._mk_client('foo') c2 = yield self._mk_client('bar') yield c1.read_message() # C2's join c2.close() response = yield c1.read_message() self.assertEqual( { 'sender': c2.config['id'], 'kind': 'part', 'body': None }, json.loads(response)) c1.close() # Wait for the channel to be removed. yield gen.sleep(0.001) with self.assertRaises(ChannelDoesNotExist): Channels.get(self.channel_id)
def setUp(self): super(BaseAsyncTest, self).setUp() Channels.reset() application = make_application() server = httpserver.HTTPServer(application) socket, self.port = testing.bind_unused_port() server.add_socket(socket)
def post(self): channel_id = self.get_argument('id') meta = self.get_argument('meta') try: Channels.create(channel_id, meta) self.write({"id": channel_id, "meta": meta}) except ChannelAlreadyExists: self.set_status(409) # Conflict self.write({"error": "The requested channel already exists."})
def test_remove(self): Channels.create(self.channel_id, self.meta) self.assertEqual(1, Channels.count()) Channels.remove(self.channel_id) self.assertEqual(0, Channels.count()) Channels.remove(self.channel_id) self.assertEqual(0, Channels.count())
def on_close(self): if not self.channel: return self.channel.connections -= 1 if self.channel.has(self): self.channel.part(self) self.broadcast(Message(MT.part, sender=self)) if self.channel.connections <= 0: L.debug('Deleting channel: %s', self.channel_id) Channels.remove(self.channel_id)
def post(self): channel_id = self.get_argument('id') meta = self.get_argument('meta') try: Channels.create(channel_id, meta) self.write({ "id": channel_id, "meta": meta }) except ChannelAlreadyExists: self.set_status(409) # Conflict self.write({ "error": "The requested channel already exists." })
def test_join(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c1 = yield self._mk_client('foo') yield self._mk_client('bar') yield c1.read_message() # C2's join self.assertEqual(2, len(channel.clients))
def test_join_part(self): channel = Channels.create(self.channel_id, self.meta) channel.join(self.member) self.assertTrue(channel.has(self.member)) self.assertEqual({ self.member.id: self.member.nick }, channel.members) channel.part(self.member) self.assertEqual({}, channel.members)
def test_nick(self): channel = Channels.get(self.channel_id) nick_1 = 'foo' nick_2 = 'bar' c1 = yield self._mk_client(nick_1) yield self._mk_client(nick_2) yield c1.read_message() # C2's join self.assertEqual({nick_1, nick_2}, set(channel.members.values()))
def test_stats(self): channel = Channels.create("test-channel", "common") response = json.loads(self.fetch('/channels/').body.decode('utf8')) self.assertEqual({"connections": 0, "channels": 1}, response) channel.connections += 1 response = json.loads(self.fetch('/channels/').body.decode('utf8')) self.assertEqual({"connections": 1, "channels": 1}, response)
def test_existing(self): self.assertFalse(Channels.exists(self.channel_id)) Channels.create(self.channel_id, self.meta) self.assertTrue(Channels.exists(self.channel_id)) with self.assertRaises(ChannelAlreadyExists): Channels.create(self.channel_id, self.meta)
def test_ping(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c = yield self._mk_client('foo') send(c, m('ping')) response = yield c.read_message() self.assertEqual({ "kind": "pong", "body": None, "sender": None }, json.loads(response))
def open(self, channel_id): self.channel_id = channel_id try: self.channel = Channels.get(self.channel_id) self.id = self.channel.new_id() # Tell the user their id and the channel's meta data. Message(MT.config, body={ "id": self.id, "meta": self.channel.meta }).send(self) self.channel.connections += 1 except ChannelDoesNotExist: self.respond_with_error("Channel does not exist.") self.close()
def test_nick_change(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c1 = yield self._mk_client('foo') c2 = yield self._mk_client('bar') send(c1, m('nick', 'foo-new')) response = yield c2.read_message() self.assertEqual({ "kind": "nick", "body": "foo-new", "sender": c1.config['id'] }, json.loads(response))
def test_part(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c1 = yield self._mk_client('foo') c2 = yield self._mk_client('bar') yield c1.read_message() # C2's join c2.close() response = yield c1.read_message() self.assertEqual({ 'sender': c2.config['id'], 'kind': 'part', 'body': None }, json.loads(response)) c1.close() # Wait for the channel to be removed. yield gen.sleep(0.001) with self.assertRaises(ChannelDoesNotExist): Channels.get(self.channel_id)
def test_nick_change(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c1 = yield self._mk_client('foo') c2 = yield self._mk_client('bar') send(c1, m('nick', 'foo-new')) response = yield c2.read_message() self.assertEqual( { "kind": "nick", "body": "foo-new", "sender": c1.config['id'] }, json.loads(response))
def test_chat(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c1 = yield self._mk_client('foo') c2 = yield self._mk_client('bar') yield c1.read_message() # C2's join send(c1, m('chat', 'hey!')) response = yield c2.read_message() self.assertEqual({ 'sender': c1.config['id'], 'kind': 'chat', 'body': 'hey!' }, json.loads(response))
def test_chat(self): channel = Channels.get(self.channel_id) self.assertEqual(0, len(channel.clients)) c1 = yield self._mk_client('foo') c2 = yield self._mk_client('bar') yield c1.read_message() # C2's join send(c1, m('chat', 'hey!')) response = yield c2.read_message() self.assertEqual( { 'sender': c1.config['id'], 'kind': 'chat', 'body': 'hey!' }, json.loads(response))
def test_create(self): meta = "common" channel_id = "test-channel" body = "&".join([ "id={channel_id}", "meta={meta}", ]).format(**locals()) response = json.loads( self.fetch('/channels/new', method='POST', body=body).body.decode('utf8')) self.assertEqual({"meta": meta, "id": channel_id}, response) channel = Channels.get(channel_id) self.assertEqual(meta, channel.meta)
def test_stats(self): channel = Channels.create("test-channel", "common") response = json.loads(self.fetch( '/channels/' ).body.decode('utf8')) self.assertEqual({ "connections": 0, "channels": 1 }, response) channel.connections += 1 response = json.loads(self.fetch( '/channels/' ).body.decode('utf8')) self.assertEqual({ "connections": 1, "channels": 1 }, response)
def test_create(self): meta = "common" channel_id = "test-channel" body = "&".join([ "id={channel_id}", "meta={meta}", ]).format(**locals()) response = json.loads(self.fetch( '/channels/new', method='POST', body=body ).body.decode('utf8')) self.assertEqual({ "meta": meta, "id": channel_id }, response) channel = Channels.get(channel_id) self.assertEqual(meta, channel.meta)
def setUp(self): super(TestChatHandler, self).setUp() Channels.create(self.channel_id, self.meta)
def test_reset(self): Channels.create(self.channel_id, self.meta) self.assertTrue(Channels.exists(self.channel_id)) Channels.reset() self.assertFalse(Channels.exists(self.channel_id))
def test_channel_delete(self): key = 'test-channel' Channels.create(key, 'common') yield gen.sleep(config.cleanup_period * 1.1) self.assertFalse(Channels.exists(key))
def get(self): self.write({ "connections": Channels.connections(), "channels": Channels.count() })
def get_app(self): Channels.reset() return make_application()
def test_does_not_exist(self): with self.assertRaises(ChannelDoesNotExist): Channels.get(self.channel_id)
def test_channel_no_delete(self): key = 'test-channel' c = Channels.create(key, 'common') c.join(Mock(nick='foo')) yield gen.sleep(config.cleanup_period * 1.1) self.assertTrue(Channels.exists(key))
def test_create(self): Channels.create(self.channel_id, self.meta) channel = Channels.get(self.channel_id) self.assertEqual(self.meta, channel.meta)
def setUp(self): super(TestChannels, self).setUp() Channels.reset()