def testFlush(self): """Test _Flush()""" client = taba_client.TabaClient('c1', 1, 'http://localhost:8280/post') taba = misc_util.Bunch(name='taba_name', type='taba_type') value = 4 body = '\n'.join(['c1', '["taba_name", "taba_type", "4", 1234567890]', '']) self.mox.StubOutWithMock(taba_client.time, 'time') taba_client.time.time().AndReturn(1234567890) self.mox.StubOutWithMock( taba_client.misc_util, 'GenericFetchFromUrlToString') taba_client.misc_util.GenericFetchFromUrlToString( 'http://localhost:8280/post', post_data=body)\ .AndReturn(misc_util.Bunch(status_code=200)) self.mox.ReplayAll() client.RecordEvent(taba.name, taba.type, value) client._Flush() self.mox.VerifyAll()
def testMovingIntervalCounter(self): """Test for MovingIntervalCounter""" client = 'c1' name = 'n1' events = [('n1', 't1', [i], i) for i in xrange(1, 501)] expt_rend = ["n1\t84690.00\t180"] self.mox.StubOutWithMock(moving_interval_counter.time, 'time') moving_interval_counter.time.time().AndReturn(1) moving_interval_counter.time.time().AndReturn(500) moving_interval_counter.time.time().AndReturn(500) self.mox.ReplayAll() handler = MovingIntervalCounter() handler.server = misc_util.Bunch(GetAverageAppliedLatency=lambda: 0.0) new = handler.NewState(client, name) state = handler.FoldEvents(new, events) proj = handler.ProjectState(state) agg = handler.Aggregate([proj, proj]) agg = handler.Aggregate([agg, proj]) rend = handler.Render(name, [agg]) self.assertEqual(rend, expt_rend) self.mox.VerifyAll()
def testFlushNormal(self): """Test Flush() under normal conditions""" agent = taba_agent.TabaAgent() agent.max_buffer_size = 10 agent.max_request_events = 100 agent.max_pending_reqs = 1 agent.server_requests_pending = [0] agent.server_event_urls = [''] agent.dummy_mode = False agent.request_workers.poll_timeout_secs = 2 agent.request_workers.Start() events_1 = ['e1', 'e2'] events_2 = ['e3'] self.mox.StubOutWithMock( taba_agent.misc_util, 'GenericFetchFromUrlToString') body1 = '\n'.join(['c2', 'e3']) + '\n' request1 = zlib.compress(body1) response1 = misc_util.Bunch(status_code=200) body2 = '\n'.join(['c1', 'e1', 'e2']) + '\n' request2 = zlib.compress(body2) response2 = misc_util.Bunch(status_code=200) taba_agent.misc_util.GenericFetchFromUrlToString( '', post_data=request1) \ .AndReturn(response1) taba_agent.misc_util.GenericFetchFromUrlToString( '', post_data=request2) \ .AndReturn(response2) self.mox.ReplayAll() agent.Buffer('c1', events_1) agent.Buffer('c2', events_2) self.assertEqual(agent._CurrentBufferSize(), 3) agent.Flush() agent.request_workers.Finish() agent.request_workers.Wait() self.assertEqual(agent._CurrentBufferSize(), 0) self.mox.VerifyAll()
def testHandlePostNormal(self): """Test HandlePost under normal conditions""" body = '\n'.join(['c1', 'e1', 'e2']) + '\n' request = misc_util.Bunch(raw={ 'request_body_bytes': StringIO(body)}) self.mox.StubOutWithMock(taba_agent_handlers.global_taba_agent, 'Buffer') taba_agent_handlers.global_taba_agent.Buffer('c1', ['e1', 'e2']) self.mox.ReplayAll() taba_agent_handlers.HandlePost(request) self.mox.VerifyAll()
def testHandlePostInvalid(self): """Test HandlePost with a garbage request""" body = 'asdfasef23waeqyeagerwv34rasef23rasdfg' request = misc_util.Bunch(raw={ 'request_body_bytes': StringIO(body)}) self.mox.StubOutWithMock(taba_agent_handlers, 'juno') taba_agent_handlers.juno.status(400) taba_agent_handlers.juno.append(mox.IgnoreArg()) self.mox.ReplayAll() taba_agent_handlers.HandlePost(request) self.mox.VerifyAll()
def testHandlePostNoEvents(self): """Test HandlePost when no events are posted""" body = '\n'.join(['c1', '']) + '\n' request = misc_util.Bunch(raw={ 'request_body_bytes': StringIO(body)}) self.mox.StubOutWithMock(taba_agent_handlers, 'juno') taba_agent_handlers.juno.status(400) taba_agent_handlers.juno.append(mox.IgnoreArg()) self.mox.ReplayAll() taba_agent_handlers.HandlePost(request) self.mox.VerifyAll()
def testCommonPrefixCounterGroup(self): """Test for CommonPrefixCounterGroup""" client = 'c1' name = 'n1' events = [('n1', 't1', ['s1', 1], 100), ('n1', 't1', ['s2', 1], 100)] expt_rend = [ 'n1_s2_1m\t3.00\t3', 'n1_s2_10m\t3.00\t3', 'n1_s2_1h\t3.00\t3', 'n1_s2_1d\t3.00\t3', 'n1_s2_pct\t3.00\t3\t[(0.25, 1.00), (0.50, 1.00), (0.75, 1.00), ' '(0.90, 1.00), (0.95, 1.00), (0.99, 1.00)]', 'n1_s1_1m\t3.00\t3', 'n1_s1_10m\t3.00\t3', 'n1_s1_1h\t3.00\t3', 'n1_s1_1d\t3.00\t3', 'n1_s1_pct\t3.00\t3\t[(0.25, 1.00), (0.50, 1.00), (0.75, 1.00), ' '(0.90, 1.00), (0.95, 1.00), (0.99, 1.00)]', ] # Mock the time and random methods to make the test predictable. # # Note that this intentionally breaks percentile counter sampling so that # the test is deterministic. The sampling is tested throughly in the # the PercentileCounter unit test. self.mox.StubOutWithMock(moving_interval_counter.time, 'time') for _ in xrange(0, 8): moving_interval_counter.time.time().AndReturn(1) for _ in xrange(0, 16): moving_interval_counter.time.time().AndReturn(101) self.mox.ReplayAll() handler = CommonPrefixCounterGroup() handler.server = misc_util.Bunch(GetAverageAppliedLatency=lambda: 0.0) new = handler.NewState(client, name) state = handler.FoldEvents(new, events) proj = handler.ProjectState(state) agg = handler.Aggregate([proj, proj]) agg = handler.Aggregate([agg, proj]) rend = handler.Render(name, [agg]) for i in range(len(expt_rend)): self.assertEqual(rend[i], expt_rend[i]) self.mox.VerifyAll()
def testMovingIntervalCounterGroup(self): """Test for MovingIntervalCounterGroup""" # Define basic input parameters. client = 'c1' name = 'n1' events = [('n1', 't1', [i], i) for i in xrange(1, 86401, 50)] expt_rend = [ 'n1_1m\t259053.00\t3', 'n1_10m\t3098736.00\t36', 'n1_1h\t18268416.00\t216', 'n1_1d\t223824384.00\t5184', 'n1_pct\t223824384.00\t5184\t[(0.25, 2501.00), (0.50, 5001.00), ' '(0.75, 7501.00), (0.90, 9001.00), (0.95, 9501.00), ' '(0.99, 9901.00)]' ] # Mock the time and random methods to make the test predictable. # # Note that this intentionally breaks percentile counter sampling so that # the test is deterministic. The sampling is tested throughly in the # the PercentileCounter unit test. self.mox.StubOutWithMock(percentile_counter_native.random, 'random') for _ in xrange(0, 86400 / 50 - 200): percentile_counter_native.random.random().AndReturn(2) self.mox.StubOutWithMock(moving_interval_counter.time, 'time') for _ in xrange(0, 4): moving_interval_counter.time.time().AndReturn(1) for _ in xrange(0, 8): moving_interval_counter.time.time().AndReturn(86400) # Execute the test pathway. self.mox.ReplayAll() handler = MovingIntervalCounterGroup() handler.server = misc_util.Bunch(GetAverageAppliedLatency=lambda: 0.0) new = handler.NewState(client, name) state = handler.FoldEvents(new, events) proj = handler.ProjectState(state) agg = handler.Aggregate([proj, proj]) agg = handler.Aggregate([agg, proj]) rend = handler.Render(name, [agg]) for i in range(len(expt_rend)): self.assertEqual(rend[i], expt_rend[i]) self.mox.VerifyAll()
def testPercentileTotalsHandler(self): """Test for PercentileCounter""" client = 'c1' name = 'n1' events = [('n1', 't1', [i], i) for i in xrange(1, 201)] expt_rend = [ 'n1\t60300.00\t600\t[(0.25, 51.00), (0.50, 101.00), ' '(0.75, 151.00), (0.90, 181.00), (0.95, 191.00), (0.99, 199.00)]' ] handler = percentile_counter.PercentileCounter() handler.server = misc_util.Bunch(GetAverageAppliedLatency=lambda: 0.0) new = handler.NewState(client, name) state = handler.FoldEvents(new, events) proj = handler.ProjectState(state) agg = handler.Aggregate([proj, proj]) agg = handler.Aggregate([agg, proj]) rend = handler.Render(name, [agg]) self.assertEqual(rend, expt_rend)