def test_client_server_full_flow(self): storage = MemoryStorage() self.server = msgpackrpc.Server(SifrServer(storage), unpack_encoding='utf-8') port = get_free_port() self.server.listen(msgpackrpc.Address('127.0.0.1', port)) self.server_thread = threading.Thread(target=self.run_server) self.server_thread.start() time.sleep(0.1) cli = RPCClient('127.0.0.1', port) cli.incr("foo", 1) self.assertEqual(1, storage.count(Hour(datetime.datetime.now(), ["foo"]))) cli.incr("bar", 1, ["minute"]) self.assertEqual(0, storage.count(Hour(datetime.datetime.now(), ["bar"]))) self.assertEqual( 1, storage.count(Minute(datetime.datetime.now(), ["bar"]))) self.assertEqual(1, cli.count("foo", datetime.datetime.now(), "hour")) cli.incr_unique("foo", "test") self.assertEqual( 1, storage.cardinality(Hour(datetime.datetime.now(), ["foo"]))) self.assertEqual( 1, cli.cardinality("foo", datetime.datetime.now(), "hour")) cli.track("foo", "test") self.assertEqual( set(["test"]), storage.uniques(Hour(datetime.datetime.now(), ["foo"]))) self.assertEqual(set(["test"]), cli.uniques("foo", datetime.datetime.now(), "hour"))
def test_incr_simple_minute(self): span = Minute(datetime.datetime.now(), ["minute_span"]) storage = RedisStorage(self.redis) storage.incr(span) storage.incr(span) self.assertEqual(storage.count(span), 2) self.assertTrue(self.redis.ttl(span.key + ":c") > 3000)
def test_multi(self): with hiro.Timeline().freeze() as timeline: storage = MemoryStorage() spans = [ Minute(datetime.datetime.now(), ["minute_span"]), Hour(datetime.datetime.now(), ["minute_span"]) ] storage.incr_multi(spans) storage.incr_unique_multi(spans, "1") storage.incr_unique_multi(spans, "2") storage.track_multi(spans, "1") storage.track_multi(spans, "2") self.assertEqual(storage.count(spans[0]), 1) self.assertEqual(storage.count(spans[1]), 1) self.assertEqual(storage.cardinality(spans[0]), 2) self.assertEqual(storage.cardinality(spans[1]), 2) self.assertEqual(storage.uniques(spans[0]), set(["1", "2"])) self.assertEqual(storage.uniques(spans[1]), set(["1", "2"])) timeline.forward((60 * 60) + 1) self.assertEqual(storage.count(spans[0]), 0) self.assertEqual(storage.count(spans[1]), 1) self.assertEqual(storage.cardinality(spans[0]), 0) self.assertEqual(storage.cardinality(spans[1]), 2) self.assertEqual(storage.uniques(spans[0]), set()) self.assertEqual(storage.uniques(spans[1]), set(["1", "2"])) timeline.forward((60 * 60 * 23) + 1) self.assertEqual(storage.count(spans[1]), 0) self.assertEqual(storage.cardinality(spans[1]), 0) self.assertEqual(storage.uniques(spans[1]), set())
def test_incr_unique_minute(self): span = Minute(datetime.datetime.now(), ["minute_span"]) storage = RiakStorage(self.riak) storage.incr_unique(span, "1") storage.incr_unique(span, "1") storage.incr_unique(span, "2") self.assertEqual(storage.cardinality(span), 2)
def test_tracker_minute(self): span = Minute(datetime.datetime.now(), ["minute_span"]) storage = RiakStorage(self.riak) storage.track(span, "1") storage.track(span, "1") storage.track(span, "2") storage.track(span, "3") self.assertEqual(storage.uniques(span), set(["1", "2", "3"]))
def test_tracker_minute(self): span = Minute(datetime.datetime.now(), ["minute_span"]) storage = RedisStorage(self.redis) storage.track(span, "1") storage.track(span, "1") storage.track(span, "2") storage.track(span, "3") self.assertEqual(storage.uniques(span), set(["1", "2", "3"])) self.assertTrue(self.redis.ttl(span.key + ":t") > 3000)
def test_incr_unique_minute(self): red = redis.Redis() span = Minute(datetime.datetime.now(), ["minute_span"]) storage = RedisStorage(red) storage.incr_unique(span, "1") storage.incr_unique(span, "1") storage.incr_unique(span, "2") self.assertEqual(storage.cardinality(span), 2) self.assertTrue(self.redis.ttl(span.key + ":u") > 3000)
def test_incr_simple_minute(self): with hiro.Timeline().freeze() as timeline: span = Minute(datetime.datetime.now(), ["minute_span"]) storage = MemoryStorage() storage.incr(span) self.assertEqual(storage.count(span), 1) storage.incr(span) self.assertEqual(storage.count(span), 2) timeline.forward((60 * 60) + 1) self.assertEqual(storage.count(span), 0)
def test_tracker_minute(self): with hiro.Timeline().freeze() as timeline: span = Minute(datetime.datetime.now(), ["minute_span"]) storage = MemoryStorage() storage.track(span, "1") storage.track(span, "1") storage.track(span, "2") storage.track(span, "3") self.assertEqual(storage.uniques(span), set(["1", "2", "3"])) timeline.forward((60 * 60) + 1) self.assertEqual(storage.uniques(span), set())
def test_incr_unique_minute(self): with hiro.Timeline().freeze() as timeline: span = Minute(datetime.datetime.now(), ["minute_span"]) storage = MemoryStorage() storage.incr_unique(span, "1") self.assertEqual(storage.cardinality(span), 1) storage.incr_unique(span, "1") self.assertEqual(storage.cardinality(span), 1) storage.incr_unique(span, "2") self.assertEqual(storage.cardinality(span), 2) timeline.forward((60 * 60) + 1) self.assertEqual(storage.cardinality(span), 0)
def test_multi(self): storage = RiakStorage(self.riak) spans = [ Minute(datetime.datetime.now(), ["minute_span"]), Hour(datetime.datetime.now(), ["minute_span"]) ] storage.incr_multi(spans) storage.incr_unique_multi(spans, "1") storage.incr_unique_multi(spans, "2") storage.track_multi(spans, "1") storage.track_multi(spans, "2") self.assertEqual(storage.count(spans[0]), 1) self.assertEqual(storage.count(spans[1]), 1) self.assertEqual(storage.cardinality(spans[0]), 2) self.assertEqual(storage.cardinality(spans[1]), 2) self.assertEqual(storage.uniques(spans[0]), set(["1", "2"])) self.assertEqual(storage.uniques(spans[1]), set(["1", "2"]))
def test_multi(self): storage = RedisStorage(self.redis) spans = [ Minute(datetime.datetime.now(), ["minute_span"]), Hour(datetime.datetime.now(), ["minute_span"]) ] storage.incr_multi(spans) storage.incr_unique_multi(spans, "1") storage.incr_unique_multi(spans, "2") storage.track_multi(spans, "1") storage.track_multi(spans, "2") self.assertEqual(storage.count(spans[0]), 1) self.assertEqual(storage.count(spans[1]), 1) self.assertEqual(storage.cardinality(spans[0]), 2) self.assertEqual(storage.cardinality(spans[1]), 2) self.assertEqual(storage.uniques(spans[0]), set(["1", "2"])) self.assertEqual(storage.uniques(spans[1]), set(["1", "2"])) self.assertTrue(self.redis.ttl(spans[0].key + ":t") > 3000) self.assertTrue(self.redis.ttl(spans[1].key + ":t") > 3599*24)
def test_minute(self): with hiro.Timeline().freeze(datetime.datetime(2012, 12, 12)): now = datetime.datetime.now() span = Minute(now, ["single"]) self.assertEqual(span.key, "single:2012-12-12_00:00") self.assertEqual(span.expiry, time.time() + 60 * 60)
def test_incr_simple_minute(self): span = Minute(datetime.datetime.now(), ["minute_span"]) storage = RiakStorage(self.riak) storage.incr(span) storage.incr(span) self.assertEqual(storage.count(span), 2)