def test_create_and_poll(self): ac_id1 = random.randint(10000000,50000000) ac_id2 = random.randint(50000001,80000000) session1a = str(uuid.uuid1()) session1b = str(uuid.uuid1()) session2a = str(uuid.uuid1()) session2b = str(uuid.uuid1()) user_id1 = random.randint(80000001,90000000) user_id2 = user_id1 + 1 user_id3 = user_id1 + 2 user_id4 = user_id1 + 3 r = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id1,user_id3,session1a)) self.assertTrue(r['success']) s1a = r['session'] self.check_session(s1a) s1b = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id1,user_id4,session1b))['session'] s2a = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id2,user_id1,session2a))['session'] s2b = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id2,user_id2,session2b))['session'] c = boto3.client('kinesis',region_name=self.props['REGION']) # msg to session1a, session1b r = self.call_gw('/notify/account/{0}'.format(ac_id1),{'msg':'test1'}) c.put_record(StreamName=self.props['EVENT_STREAM'], Data=json.dumps({'accountId':ac_id1, 'msg':'test1-k'}), PartitionKey=str(ac_id1)) # msg to session1b r = self.call_gw('/notify/user/{0}/session/{1}'.format(user_id4,session1b),{'msg':'test2'}) c.put_record(StreamName=self.props['EVENT_STREAM'], Data=json.dumps({'accountId':ac_id1, 'sessionId':session1b, '_sqs_only':1, 'msg':'test2-k'}), PartitionKey=str(ac_id1)) # msg to session2a r = self.call_gw('/notify/user/{1}'.format(ac_id2,user_id1),{'msg':'test3'}) c.put_record(StreamName=self.props['EVENT_STREAM'], Data=json.dumps({'accountId':ac_id2, 'userId':user_id1, 'msg':'test3-k'}), PartitionKey=str(ac_id2)) time.sleep(10) msgs_1a = get_msgs(s1a) msgs_1b = get_msgs(s1b) msgs_2a = get_msgs(s2a) msgs_2b = get_msgs(s2b) self.assertListEqual(msgs_1a,['test1','test1-k']) self.assertListEqual(msgs_1b,['test1','test1-k','test2','test2-k']) self.assertListEqual(msgs_2a,['test3','test3-k']) self.assertListEqual(msgs_2b,[])
def test_create_destroy(self): ac_id1 = random.randint(10000000,50000000) session1a = str(uuid.uuid1()) user_id = random.randint(0,10000000) r = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id1,user_id,session1a)) self.assertTrue(r['success']) s1a = r['session'] r = self.call_gw('/destroy/{0}/{1}/{2}'.format(ac_id1,user_id,session1a)) self.assertTrue(r['success']) r = self.call_gw('/notify/account/{0}'.format(ac_id1),{'msg':'test6'}) time.sleep(5) with self.assertRaises(Exception): get_msgs(s1a)
def test_read_receipt_msgs(self): ac_id = random.randint(10000000, 50000000) session = str(uuid.uuid1()) user_id = random.randint(80000001, 90000000) r = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id, user_id, session)) s = r['session'] time.sleep(1) self.call_gw('/notify/user/{0}'.format(user_id), {'msg': 'test1'}) self.call_gw('/notify/user/{0}'.format(user_id), {'msg': 'test2'}) self.call_gw('/notify/user/{0}'.format(user_id), {'msg': 'test3'}) time.sleep(5) r = self.call_gw('/messages/user/{0}'.format(user_id)) for m in r['messages']: self.assertEqual(m['is_read'], 0) # set read receipt r = self.call_gw('/messages/set-read/user/{0}/asof/{1}'.format( user_id, time.time())) time.sleep(0.5) r = self.call_gw('/messages/user/{0}'.format(user_id)) for m in r['messages']: self.assertEqual(m['is_read'], 1) # get sqs messages, should have a read-receipt msg present with all of the messages IDs time.sleep(10) msgs = get_msgs(s, raw=True) # filter for message-read-receipt type msgs = [x for x in msgs if x.get('_type') == 'message-read-receipt'] msg_ids = sorted([x['messageId'] for x in r['messages']]) self.assertEqual(sorted(msgs[0]['messages-receipted']), msg_ids)
def test_high_vol(self): # create 50 listeners accounts = [ (random.randint(10000000,10000010), str(uuid.uuid1()), random.randint(80000001,90000000)) for _ in xrange(50) ] s_list = [ self.call_gw('/create/{0}/{1}/{2}'.format(ac_id,user_id,s)) for ac_id,s,user_id in accounts ] c = boto3.client('kinesis',region_name=self.props['REGION']) msg_list = [] stream = self.props['EVENT_STREAM'] # create 100 broadcast messages for _ in xrange(100): msg_list.append({'StreamName':stream, 'Data':json.dumps({'msg':str(random.randint(0,1000000000))}), 'PartitionKey':str(random.randint(0,1000000000))}) # for each account, create three user messages and one account message for ac_id,s,user_id in accounts: msg_list.append({'StreamName':stream, 'Data':json.dumps({'msg':str(random.randint(0,1000000000)), 'accountId':ac_id}), 'PartitionKey':str(random.randint(0,1000000000))}) msg_list.append({'StreamName':stream, 'Data':json.dumps({'msg':str(random.randint(0,1000000000)), 'accountId':ac_id, 'userId':user_id}), 'PartitionKey':str(random.randint(0,1000000000))}) msg_list.append({'StreamName':stream, 'Data':json.dumps({'msg':str(random.randint(0,1000000000)), 'accountId':ac_id, 'userId':user_id}), 'PartitionKey':str(random.randint(0,1000000000))}) # shuffle messages random.shuffle(msg_list) for m in msg_list: c.put_record(**m) print("Messages posted, waiting for receipt") time.sleep(60) # pull one of the queues s = s_list[0]['session'] act_id = s['accountId'] user_id = s['userId'] queue_items = get_msgs(s) # what should be in there? msg_d_list = [ json.loads(x['Data']) for x in msg_list ] s_msg_list = [ x['msg'] for x in msg_d_list if x.get('accountId') in (None,act_id) or x.get('userId')==user_id ] s_msg_list.sort() json.dump(queue_items,open('msg_list','w')) json.dump(s_msg_list,open('s_msg_list','w')) self.assertListEqual(queue_items, s_msg_list)
def test_broadcast(self): ac_id1 = random.randint(10000000,50000000) session1a = str(uuid.uuid1()) user_id1 = random.randint(80000001,90000000) s = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id1,user_id1,session1a))['session'] r = self.call_gw('/notify',{'msg':'test12'}) time.sleep(30) msgs = get_msgs(s) self.assertListEqual(msgs,['test12'])
def test_user_dispatch(self): ac_id1 = random.randint(10000000,50000000) session1a = str(uuid.uuid1()) user_id1 = random.randint(80000001,90000000) s = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id1,user_id1,session1a))['session'] r = self.call_gw('/notify/user/{0}'.format(user_id1),{'msg':'test9'}) time.sleep(5) msgs = get_msgs(s) self.assertListEqual(msgs,['test9'])
def test_double_create(self): ac_id1 = random.randint(10000000,50000000) session1a = str(uuid.uuid1()) user_id = random.randint(0,10000000) r = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id1,user_id,session1a)) self.assertTrue(r['success']) r = self.call_gw('/create/{0}/{1}/{2}'.format(ac_id1,user_id,session1a)) self.assertTrue(r['success']) s1a = r['session'] r = self.call_gw('/notify/account/{0}'.format(ac_id1),{'msg':'test6'}) time.sleep(5) msgs_1a = get_msgs(s1a) self.assertListEqual(msgs_1a,['test6'])