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