summaryrefslogtreecommitdiffstats
path: root/tests/carbon_tests/emitter_test.py
blob: fe19ed2b6d909e12efc0ae0a33898bd939ceb571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
##############################################################################
# Copyright (c) 2015 EMC and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from storperf.carbon import converter
from storperf.carbon.emitter import CarbonMetricTransmitter
from time import sleep
import SocketServer
import json
import threading
import unittest


class MetricsHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        # Echo the back to the client
        CarbonMetricTransmitterTest.response = self.request.recv(1024)
        return


class MetricsServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass


class CarbonMetricTransmitterTest(unittest.TestCase):
    listen_port = 0
    response = None

    def setUp(self):

        address = ('localhost', 0)
        server = MetricsServer(address, MetricsHandler)
        ip, self.listen_port = server.server_address

        t = threading.Thread(target=server.serve_forever)
        t.setDaemon(True)
        t.start()

    def test_transmit_metrics(self):

        testconv = converter.Converter()
        json_object = json.loads("""{"timestamp" : "12345", "key":"value" }""")
        result = testconv.convert_json_to_flat(json_object, "host.run-name")

        emitter = CarbonMetricTransmitter()
        emitter.carbon_port = self.listen_port
        emitter.transmit_metrics(result)

        count = 0

        while (CarbonMetricTransmitterTest.response is None and count < 10):
            count += 1
            sleep(0.1)

        self.assertEqual("host.run-name.key value 12345\n",
                         CarbonMetricTransmitterTest.response,
                         CarbonMetricTransmitterTest.response)

if __name__ == '__main__':
    unittest.main()