|
1 | 1 | import os |
| 2 | +import pytest |
2 | 3 |
|
3 | 4 |
|
4 | 5 | class TestSafeLiteralEval: |
@@ -151,3 +152,84 @@ def test_returns_default_when_file_missing(self, temp_dir): |
151 | 152 | assert result == [5, 5] |
152 | 153 | concoredocker.inpath = old_inpath |
153 | 154 | concoredocker.delay = old_delay |
| 155 | + |
| 156 | + |
| 157 | +class TestZMQ: |
| 158 | + @pytest.fixture(autouse=True) |
| 159 | + def reset_zmq_ports(self): |
| 160 | + import concoredocker |
| 161 | + original_ports = concoredocker.zmq_ports.copy() |
| 162 | + yield |
| 163 | + concoredocker.zmq_ports.clear() |
| 164 | + concoredocker.zmq_ports.update(original_ports) |
| 165 | + |
| 166 | + def test_write_prepends_simtime(self): |
| 167 | + import concoredocker |
| 168 | + |
| 169 | + class DummyPort: |
| 170 | + def __init__(self): |
| 171 | + self.sent = None |
| 172 | + |
| 173 | + def send_json_with_retry(self, message): |
| 174 | + self.sent = message |
| 175 | + |
| 176 | + dummy = DummyPort() |
| 177 | + concoredocker.zmq_ports["test_zmq"] = dummy |
| 178 | + concoredocker.simtime = 3.0 |
| 179 | + |
| 180 | + concoredocker.write("test_zmq", "data", [1.0, 2.0], delta=2) |
| 181 | + |
| 182 | + assert dummy.sent == [5.0, 1.0, 2.0] |
| 183 | + assert concoredocker.simtime == 5.0 |
| 184 | + |
| 185 | + def test_read_strips_simtime(self): |
| 186 | + import concoredocker |
| 187 | + |
| 188 | + class DummyPort: |
| 189 | + def recv_json_with_retry(self): |
| 190 | + return [10.0, 4.0, 5.0] |
| 191 | + |
| 192 | + concoredocker.zmq_ports["test_zmq"] = DummyPort() |
| 193 | + concoredocker.simtime = 0 |
| 194 | + |
| 195 | + result = concoredocker.read("test_zmq", "data", "[]") |
| 196 | + |
| 197 | + assert result == [4.0, 5.0] |
| 198 | + assert concoredocker.simtime == 10.0 |
| 199 | + |
| 200 | + def test_read_updates_simtime_monotonically(self): |
| 201 | + import concoredocker |
| 202 | + |
| 203 | + class DummyPort: |
| 204 | + def recv_json_with_retry(self): |
| 205 | + return [2.0, 99.0] |
| 206 | + |
| 207 | + concoredocker.zmq_ports["test_zmq"] = DummyPort() |
| 208 | + concoredocker.simtime = 5.0 |
| 209 | + |
| 210 | + concoredocker.read("test_zmq", "data", "[]") |
| 211 | + |
| 212 | + assert concoredocker.simtime == 5.0 |
| 213 | + |
| 214 | + def test_write_read_roundtrip(self): |
| 215 | + import concoredocker |
| 216 | + |
| 217 | + class DummyPort: |
| 218 | + def __init__(self): |
| 219 | + self.buffer = None |
| 220 | + |
| 221 | + def send_json_with_retry(self, message): |
| 222 | + self.buffer = message |
| 223 | + |
| 224 | + def recv_json_with_retry(self): |
| 225 | + return self.buffer |
| 226 | + |
| 227 | + dummy = DummyPort() |
| 228 | + concoredocker.zmq_ports["roundtrip"] = dummy |
| 229 | + concoredocker.simtime = 0 |
| 230 | + |
| 231 | + original = [1.5, 2.5, 3.5] |
| 232 | + concoredocker.write("roundtrip", "data", original) |
| 233 | + result = concoredocker.read("roundtrip", "data", "[]") |
| 234 | + |
| 235 | + assert result == original |
0 commit comments