def test_rss2rw_update_message_timeout_append(self): # We can not make the RSS feed go arbitrary long. # Timeout-ed entried will be deleted upon every update operation. # This UT tests the behaviour when appending a timeout-ed item. import time _cur_time = int(time.time()) # Use the generic Message instead of RSS2RW.Message msg = snstype.Message() msg.parsed.username = "******" msg.parsed.userid = "test_username" msg.parsed.time = _cur_time msg.parsed.text = "test status" self.rss.update(msg) self.rss.update(msg) eq_(len(self.rss.home_timeline()), 2) self.rss.update(msg) eq_(len(self.rss.home_timeline()), 3) # 1 second before timeout msg.parsed.time -= self.rss.jsonconf.entry_timeout - 1 self.rss.update(msg) eq_(len(self.rss.home_timeline()), 4) # 1 second after timeout # Should reject this entry msg.parsed.time -= 2 self.rss.update(msg) eq_(len(self.rss.home_timeline()), 4)
def test_rss2rw_update_message_make_link(self): # Check the link is correctly generated # See ``_make_link`` for more info. # None: no timeout; keep all entries permanently self.rss.jsonconf.entry_timeout = None msg = snstype.Message() msg.parsed.username = "******" msg.parsed.userid = "test_username" msg.parsed.text = "test status" msg.parsed.time = 1234567890 self.rss.update(msg) msg2 = self.rss.home_timeline()[0] eq_(msg2.parsed.link, 'http://goo.gl/7aokV#a6dd6e622b2b4f01065b6abe47571a33423a16ea')
def test_rss2rw_update_message(self): import time # Use the generic Message instead of RSS2RW.Message msg = snstype.Message() msg.parsed.username = "******" # Current RSS feeds do not distinguish userid and username # In the future, userid may be coded into our special structure msg.parsed.userid = "test_username" msg.parsed.time = int(time.time()) msg.parsed.text = "test status" self.rss.update(msg) msg2 = self.rss.home_timeline()[0] eq_(msg.parsed.time, msg2.parsed.time) eq_(msg.parsed.username, msg2.parsed.username) eq_(msg.parsed.userid, msg2.parsed.userid) eq_(msg.parsed.text, msg2.parsed.text)
def test_rss2rw_update_message_timeout_simulate(self): # This UT simulates a timeout scenario import time _cur_time = int(time.time()) # Use the generic Message instead of RSS2RW.Message msg = snstype.Message() msg.parsed.username = "******" msg.parsed.userid = "test_username" msg.parsed.time = _cur_time msg.parsed.text = "test status" # Normal update self.rss.update(msg) eq_(len(self.rss.home_timeline()), 1) # Change our timer _new_time = _cur_time + self.rss.jsonconf.entry_timeout + 1 time.time = lambda: _new_time msg.parsed.time = int(time.time()) self.rss.update(msg) # The previous message is kicked out eq_(len(self.rss.home_timeline()), 1)
if 'user_name' in ch.jsonconf: user_name = ch.jsonconf['user_name'] if user_name == msg.parsed.username: mymsg = True if mymsg: logger.debug('distribute message: %s', msg) distribute.update(msg) if __name__ == '__main__': from snsapi import snspocket, snstype sp = snspocket.SNSPocket() sp.add_channel(distribute_conf) #sp.load_config() sp.auth() from queue import SRFEQueue q = SRFEQueue(sp) q.connect() q.refresh_tags() import time m1 = snstype.Message({ 'text': 'test', 'username': '******', 'userid': 'hpl', 'time': time.time() }) #hook_new_message(q, m1) q._inqueue(m1)