|
| 1 | +import re |
| 2 | +import time |
| 3 | +# import unittest |
| 4 | +import threading |
| 5 | +from nose import tools as t |
| 6 | + |
| 7 | +from datadog import lambda_stats, datadog_lambda_wrapper |
| 8 | + |
| 9 | + |
| 10 | +class MemoryReporter(object): |
| 11 | + """ A reporting class that reports to memory for testing. """ |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.metrics = [] |
| 15 | + self.events = [] |
| 16 | + |
| 17 | + def flush_metrics(self, metrics): |
| 18 | + self.metrics += metrics |
| 19 | + |
| 20 | + def flush_events(self, events): |
| 21 | + self.events += events |
| 22 | + |
| 23 | + |
| 24 | +@datadog_lambda_wrapper |
| 25 | +def wrapped_function(id): |
| 26 | + |
| 27 | + t.assert_greater(datadog_lambda_wrapper._counter, 1) |
| 28 | + # Increment |
| 29 | + lambda_stats.increment("counter", timestamp=12345) |
| 30 | + time.sleep(0.001) # sleep makes the os continue another thread |
| 31 | + |
| 32 | + # Gauge |
| 33 | + lambda_stats.gauge("gauge_" + str(id), 42) |
| 34 | + time.sleep(0.001) # sleep makes the os continue another thread |
| 35 | + |
| 36 | + # Histogram |
| 37 | + lambda_stats.histogram("histogram", id, timestamp=12345) |
| 38 | + time.sleep(0.001) # sleep makes the os continue another thread |
| 39 | + |
| 40 | + # Event |
| 41 | + lambda_stats.event("title", "content") |
| 42 | + |
| 43 | + |
| 44 | +@datadog_lambda_wrapper |
| 45 | +def wrapped_init(): |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +class TestWrapperThreadSafety(object): |
| 50 | + |
| 51 | + def test_wrapper_thread_safety(self): |
| 52 | + |
| 53 | + wrapped_init() # Empty run to make the initialization |
| 54 | + lambda_stats.reporter = MemoryReporter() |
| 55 | + datadog_lambda_wrapper._counter = 1 |
| 56 | + |
| 57 | + for i in range(10000): |
| 58 | + threading.Thread(target=wrapped_function, args=[i]).start() |
| 59 | + # Wait all threads to finish |
| 60 | + time.sleep(10) |
| 61 | + |
| 62 | + # Flush and check |
| 63 | + t.assert_equal(datadog_lambda_wrapper._counter, 1) |
| 64 | + lambda_stats.flush(float("inf")) |
| 65 | + |
| 66 | + metrics = lambda_stats.reporter.metrics |
| 67 | + events = lambda_stats.reporter.events |
| 68 | + |
| 69 | + # Overview |
| 70 | + t.assert_equal(len(metrics), 10009, len(metrics)) |
| 71 | + |
| 72 | + # Sort metrics |
| 73 | + counter_metrics = [] |
| 74 | + gauge_metrics = [] |
| 75 | + histogram_metrics = [] |
| 76 | + |
| 77 | + for m in metrics: |
| 78 | + if re.match("gauge_.*", m['metric']): |
| 79 | + gauge_metrics.append(m) |
| 80 | + elif re.match("histogram.*", m['metric']): |
| 81 | + histogram_metrics.append(m) |
| 82 | + else: |
| 83 | + counter_metrics.append(m) |
| 84 | + |
| 85 | + # Counter |
| 86 | + t.assert_equal(len(counter_metrics), 1, len(counter_metrics)) |
| 87 | + counter = counter_metrics[0] |
| 88 | + t.assert_equal(counter['points'][0][1], 10000, counter['points'][0][1]) |
| 89 | + |
| 90 | + # Gauge |
| 91 | + t.assert_equal(len(gauge_metrics), 10000, len(gauge_metrics)) |
| 92 | + |
| 93 | + # Histogram |
| 94 | + t.assert_equal(len(histogram_metrics), 8, len(histogram_metrics)) |
| 95 | + count_histogram = filter(lambda x: x['metric'] == "histogram.count", histogram_metrics)[0] |
| 96 | + t.assert_equal(count_histogram['points'][0][1], 10000, count_histogram['points'][0][1]) |
| 97 | + sum_histogram = filter(lambda x: x['metric'] == "histogram.avg", histogram_metrics)[0] |
| 98 | + t.assert_equal(sum_histogram['points'][0][1], 4999.5, sum_histogram['points'][0][1]) |
| 99 | + |
| 100 | + # Events |
| 101 | + t.assert_equal(10000, len(events), len(events)) |
0 commit comments