|
1 | 1 | import itertools
|
| 2 | +import os |
| 3 | +import shutil |
2 | 4 | import socket
|
| 5 | +import tempfile |
3 | 6 | from threading import Thread
|
| 7 | +import uuid |
4 | 8 |
|
5 | 9 | import pytest
|
6 | 10 |
|
7 | 11 | from datadog.dogstatsd.base import DogStatsd
|
8 | 12 |
|
9 | 13 | @pytest.mark.parametrize(
|
10 |
| - "disable_background_sender, disable_buffering, wait_for_pending, socket_timeout, stop", |
11 |
| - list(itertools.product([True, False], [True, False], [True, False], [0, 1], [True, False])), |
| 14 | + "disable_background_sender, disable_buffering, wait_for_pending, socket_timeout, stop, socket_kind", |
| 15 | + list(itertools.product([True, False], [True, False], [True, False], [0, 1], [True, False], [socket.SOCK_DGRAM, socket.SOCK_STREAM])), |
12 | 16 | )
|
13 |
| -def test_sender_mode(disable_background_sender, disable_buffering, wait_for_pending, socket_timeout, stop): |
| 17 | +def test_sender_mode(disable_background_sender, disable_buffering, wait_for_pending, socket_timeout, stop, socket_kind): |
14 | 18 | # Test basic sender operation with an assortment of options
|
15 |
| - foo, bar = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM, 0) |
| 19 | + foo, bar = socket.socketpair(socket.AF_UNIX, socket_kind, 0) |
16 | 20 | statsd = DogStatsd(
|
17 | 21 | telemetry_min_flush_interval=0,
|
18 | 22 | disable_background_sender=disable_background_sender,
|
@@ -101,3 +105,42 @@ def test_buffering_with_context():
|
101 | 105 | bar.settimeout(5)
|
102 | 106 | msg = bar.recv(8192)
|
103 | 107 | assert msg == b"first:1|c\n"
|
| 108 | + |
| 109 | +@pytest.fixture() |
| 110 | +def socket_dir(): |
| 111 | + tempdir = tempfile.mkdtemp() |
| 112 | + yield tempdir |
| 113 | + shutil.rmtree(tempdir) |
| 114 | + |
| 115 | +@pytest.mark.parametrize( |
| 116 | + "socket_prefix, socket_kind, success", |
| 117 | + [ |
| 118 | + ("", socket.SOCK_DGRAM, True), |
| 119 | + ("", socket.SOCK_STREAM, True), |
| 120 | + ("unix://", socket.SOCK_DGRAM, True), |
| 121 | + ("unix://", socket.SOCK_STREAM, True), |
| 122 | + ("unixstream://", socket.SOCK_DGRAM, False), |
| 123 | + ("unixstream://", socket.SOCK_STREAM, True), |
| 124 | + ("unixgram://", socket.SOCK_DGRAM, True), |
| 125 | + ("unixgram://", socket.SOCK_STREAM, False) |
| 126 | + ] |
| 127 | +) |
| 128 | +def test_socket_connection(socket_dir, socket_prefix, socket_kind, success): |
| 129 | + socket_path = os.path.join(socket_dir, str(uuid.uuid1()) + ".sock") |
| 130 | + listener_socket = socket.socket(socket.AF_UNIX, socket_kind) |
| 131 | + listener_socket.bind(socket_path) |
| 132 | + |
| 133 | + if socket_kind == socket.SOCK_STREAM: |
| 134 | + listener_socket.listen(1) |
| 135 | + |
| 136 | + statsd = DogStatsd( |
| 137 | + socket_path = socket_prefix + socket_path |
| 138 | + ) |
| 139 | + |
| 140 | + if success: |
| 141 | + assert statsd.get_socket() is not None |
| 142 | + else: |
| 143 | + with pytest.raises(OSError): |
| 144 | + statsd.get_socket() |
| 145 | + |
| 146 | + listener_socket.close() |
0 commit comments