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