intempt 3.0.0__tar.gz → 3.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- intempt-3.1.0/.claude/lane.json +1 -0
- {intempt-3.0.0 → intempt-3.1.0}/ARCHITECTURE.md +7 -4
- {intempt-3.0.0 → intempt-3.1.0}/CHANGELOG.md +23 -1
- {intempt-3.0.0 → intempt-3.1.0}/PKG-INFO +9 -5
- {intempt-3.0.0 → intempt-3.1.0}/README.md +8 -4
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/__init__.py +1 -1
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_client.py +6 -5
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_delivery.py +1 -1
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_transport.py +65 -36
- {intempt-3.0.0 → intempt-3.1.0}/tests/conftest.py +21 -4
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_client.py +22 -4
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_delivery_failure.py +4 -2
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_transport.py +64 -6
- {intempt-3.0.0 → intempt-3.1.0}/.github/workflows/autopr.yaml +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/.github/workflows/blu-bot-pipeline-caller.yml +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/.github/workflows/e2e.yml +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/.github/workflows/ff-merge.yml +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/.github/workflows/release.yml +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/.github/workflows/tests.yml +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/.gitignore +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/LICENSE +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/NOTICE +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/docs/CONVENTIONS.md +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/docs/TESTING.md +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/examples/README.md +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/examples/bare/send.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/examples/basic/app.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/pyproject.toml +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/scripts/check-no-local-bucketing.mjs +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/scripts/e2e.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/scripts/mutation_gate.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/scripts/no-local-bucketing-allow.json +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_buffer.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_config.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_errors.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_flags.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/src/intempt/_util.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/__init__.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_bare_sample.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_bucketing_guard.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_buffer.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_config.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_credential_guard.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_e2e_script.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_example_app.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_flags.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_messages.py +0 -0
- {intempt-3.0.0 → intempt-3.1.0}/tests/test_thresholds.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"lane":"BUG","ref":"INT-7291","test":"tests/test_transport.py"}
|
|
@@ -37,7 +37,7 @@ Identical across all three, allowing for language idiom (`snake_case` in Python,
|
|
|
37
37
|
| `consent.grant(options)` / `consent.revoke(options)` | `POST …/consents/data` |
|
|
38
38
|
| `ecommerce.product_viewed / added_to_cart / ordered` | `POST …/track` |
|
|
39
39
|
| `recommend(options)` | `POST …/feeds/{feedId}/data` |
|
|
40
|
-
| `opt_in()` / `opt_out()` / `is_opted_in()` | — |
|
|
40
|
+
| `opt_in()` / `opt_out()` / `is_opted_in()` / `has_opted_out()` | — |
|
|
41
41
|
| `flush()` / `close()` | — |
|
|
42
42
|
| `set_config(patch)` / `config` / `buffered` | — |
|
|
43
43
|
|
|
@@ -100,9 +100,12 @@ different source. Never coerce an id numerically, in any language, at any layer.
|
|
|
100
100
|
|
|
101
101
|
### Delivery is at-least-once
|
|
102
102
|
|
|
103
|
-
Ingestion has no idempotency key.
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
Ingestion has no idempotency key. Inside one request, payload items that share an
|
|
104
|
+
`event_id` are stored as one row: measured 2026-11-16 (INT-5415, INT-5410), an order
|
|
105
|
+
with two lines and one shared id stored one row, and with an id per line it stored two.
|
|
106
|
+
So every payload item carries its own `event_id`. Whether a resend of a request that
|
|
107
|
+
already landed is collapsed is not measured, so the SDK assumes it is not. That is why
|
|
108
|
+
nothing that may have landed is resent.
|
|
106
109
|
|
|
107
110
|
### close() is bounded
|
|
108
111
|
|
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 3.
|
|
3
|
+
## 3.1.0 — unreleased
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- **`has_opted_out()`** (INT-5416), the inverse of `is_opted_in()`: true after
|
|
8
|
+
`opt_out()` and after `close()`. The public docs already name it.
|
|
9
|
+
|
|
10
|
+
### Changed — behaviour
|
|
11
|
+
|
|
12
|
+
- **A `datetime` or `date` inside `properties`, `user_attributes` or
|
|
13
|
+
`account_attributes` is sent as an ISO-8601 UTC string with milliseconds**
|
|
14
|
+
(INT-5418), `YYYY-MM-DDTHH:MM:SS.sssZ`, as the Swift, JS, Node and React Native SDKs
|
|
15
|
+
send it. Before, a `datetime` went out as epoch milliseconds and a `date` raised
|
|
16
|
+
`TypeError`. A naive `datetime` is read as UTC (it was read as local time); a `date`
|
|
17
|
+
is midnight UTC. The event `timestamp` option is unchanged and stays epoch
|
|
18
|
+
milliseconds.
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
- **`ecommerce.ordered()` gives each product line its own `eventId`** (INT-5415).
|
|
23
|
+
The lines shared one id, and the server kept only one line of a multi-product order.
|
|
24
|
+
|
|
25
|
+
## 3.0.0 — 2026-11-13
|
|
4
26
|
|
|
5
27
|
### Changed — breaking
|
|
6
28
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: intempt
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.1.0
|
|
4
4
|
Summary: Intempt Python SDK — server-side. Data in, decisions out.
|
|
5
5
|
Project-URL: Homepage, https://intempt.com
|
|
6
6
|
Project-URL: Source, https://github.com/intempt/intempt-python
|
|
@@ -82,7 +82,7 @@ other short-lived processes.
|
|
|
82
82
|
| `ecommerce.added_to_cart(product_id=…, quantity=…, **ids)` | `bool` | `POST …/track` |
|
|
83
83
|
| `ecommerce.ordered(products=[…], **ids)` | `bool` | `POST …/track` |
|
|
84
84
|
| `recommend(feed_id=…, fields=[…], **ids)` | `dict` | `POST …/feeds/{id}/data` |
|
|
85
|
-
| `opt_in()` / `opt_out()` / `is_opted_in()` | — | — |
|
|
85
|
+
| `opt_in()` / `opt_out()` / `is_opted_in()` / `has_opted_out()` | — | — |
|
|
86
86
|
| `flush()` / `close()` | `FlushResult(sent, failed)` | — |
|
|
87
87
|
| `set_config(**patch)` / `config` / `buffered` | — | — |
|
|
88
88
|
|
|
@@ -158,9 +158,9 @@ At `max_queue` the oldest queued event that no send is carrying is dropped and r
|
|
|
158
158
|
|
|
159
159
|
The buffer is in memory. A hard crash loses it.
|
|
160
160
|
|
|
161
|
-
**Ingestion has no idempotency key
|
|
162
|
-
|
|
163
|
-
|
|
161
|
+
**Ingestion has no idempotency key.** Payload items of one request that share an
|
|
162
|
+
`eventId` are stored as one row (measured, INT-5415), so each item gets its own id.
|
|
163
|
+
Whether a resend of a request that already landed is collapsed is not measured. That is why a request that may have landed (a timeout after the write) is
|
|
164
164
|
never resent: the SDK trades a possible loss, which it reports, for no duplicates.
|
|
165
165
|
|
|
166
166
|
## Timestamps
|
|
@@ -168,6 +168,10 @@ never resent: the SDK trades a possible loss, which it reports, for no duplicate
|
|
|
168
168
|
`timestamp` accepts a `datetime` or epoch milliseconds. A naive `datetime` is treated
|
|
169
169
|
as UTC, not local time.
|
|
170
170
|
|
|
171
|
+
A `datetime` or `date` inside `properties` or the attribute maps is sent as an ISO-8601
|
|
172
|
+
UTC string with milliseconds (`2026-01-01T09:30:05.123Z`), the same as the other SDKs.
|
|
173
|
+
Naive is UTC there too; a `date` is midnight UTC.
|
|
174
|
+
|
|
171
175
|
It **is** a backfill mechanism, between 2010 and 2040. The event store keeps your value
|
|
172
176
|
and records arrival separately, so the two never overwrite each other.
|
|
173
177
|
|
|
@@ -55,7 +55,7 @@ other short-lived processes.
|
|
|
55
55
|
| `ecommerce.added_to_cart(product_id=…, quantity=…, **ids)` | `bool` | `POST …/track` |
|
|
56
56
|
| `ecommerce.ordered(products=[…], **ids)` | `bool` | `POST …/track` |
|
|
57
57
|
| `recommend(feed_id=…, fields=[…], **ids)` | `dict` | `POST …/feeds/{id}/data` |
|
|
58
|
-
| `opt_in()` / `opt_out()` / `is_opted_in()` | — | — |
|
|
58
|
+
| `opt_in()` / `opt_out()` / `is_opted_in()` / `has_opted_out()` | — | — |
|
|
59
59
|
| `flush()` / `close()` | `FlushResult(sent, failed)` | — |
|
|
60
60
|
| `set_config(**patch)` / `config` / `buffered` | — | — |
|
|
61
61
|
|
|
@@ -131,9 +131,9 @@ At `max_queue` the oldest queued event that no send is carrying is dropped and r
|
|
|
131
131
|
|
|
132
132
|
The buffer is in memory. A hard crash loses it.
|
|
133
133
|
|
|
134
|
-
**Ingestion has no idempotency key
|
|
135
|
-
|
|
136
|
-
|
|
134
|
+
**Ingestion has no idempotency key.** Payload items of one request that share an
|
|
135
|
+
`eventId` are stored as one row (measured, INT-5415), so each item gets its own id.
|
|
136
|
+
Whether a resend of a request that already landed is collapsed is not measured. That is why a request that may have landed (a timeout after the write) is
|
|
137
137
|
never resent: the SDK trades a possible loss, which it reports, for no duplicates.
|
|
138
138
|
|
|
139
139
|
## Timestamps
|
|
@@ -141,6 +141,10 @@ never resent: the SDK trades a possible loss, which it reports, for no duplicate
|
|
|
141
141
|
`timestamp` accepts a `datetime` or epoch milliseconds. A naive `datetime` is treated
|
|
142
142
|
as UTC, not local time.
|
|
143
143
|
|
|
144
|
+
A `datetime` or `date` inside `properties` or the attribute maps is sent as an ISO-8601
|
|
145
|
+
UTC string with milliseconds (`2026-01-01T09:30:05.123Z`), the same as the other SDKs.
|
|
146
|
+
Naive is UTC there too; a `date` is midnight UTC.
|
|
147
|
+
|
|
144
148
|
It **is** a backfill mechanism, between 2010 and 2040. The event store keeps your value
|
|
145
149
|
and records arrival separately, so the two never overwrite each other.
|
|
146
150
|
|
|
@@ -16,7 +16,7 @@ from ._errors import IntemptApiError, IntemptConfigError, IntemptError
|
|
|
16
16
|
from ._flags import FlagContext, FlagDetail, FlagError, FlagReason
|
|
17
17
|
from ._transport import ApiKeyCredentials, Transport
|
|
18
18
|
|
|
19
|
-
__version__ = "3.
|
|
19
|
+
__version__ = "3.1.0"
|
|
20
20
|
|
|
21
21
|
__all__ = [
|
|
22
22
|
"Intempt",
|
|
@@ -556,6 +556,9 @@ class Intempt:
|
|
|
556
556
|
def is_opted_in(self) -> bool:
|
|
557
557
|
return self._opted_in and not self._closed
|
|
558
558
|
|
|
559
|
+
def has_opted_out(self) -> bool:
|
|
560
|
+
return not self.is_opted_in()
|
|
561
|
+
|
|
559
562
|
# -- config -----------------------------------------------------------
|
|
560
563
|
|
|
561
564
|
def set_config(self, **patch: Any) -> None:
|
|
@@ -657,17 +660,15 @@ class Intempt:
|
|
|
657
660
|
) -> bool:
|
|
658
661
|
"""One event carrying several payload items, one per line.
|
|
659
662
|
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
cannot collapse rows.
|
|
663
|
+
Each line gets its own eventId (INT-5415): with a shared id the server
|
|
664
|
+
kept only one line of a multi-product order.
|
|
663
665
|
"""
|
|
664
|
-
event_id = str(uuid.uuid4())
|
|
665
666
|
raw = ids.get("timestamp")
|
|
666
667
|
millis = ensure_timestamp(raw) if raw is not None else _now_ms()
|
|
667
668
|
payload = [
|
|
668
669
|
compact(
|
|
669
670
|
{
|
|
670
|
-
"eventId":
|
|
671
|
+
"eventId": str(uuid.uuid4()),
|
|
671
672
|
"timestamp": millis,
|
|
672
673
|
"profileId": ids.get("profile_id"),
|
|
673
674
|
"userId": ids.get("user_id"),
|
|
@@ -71,7 +71,7 @@ Outcome = Literal["sent", "kept", "dropped", "discarded"]
|
|
|
71
71
|
|
|
72
72
|
|
|
73
73
|
def event_ids(events: Sequence[dict[str, Any]]) -> tuple[str, ...]:
|
|
74
|
-
"""Distinct ids in order
|
|
74
|
+
"""Distinct ids in order; an id repeated across events is reported once."""
|
|
75
75
|
seen: dict[str, None] = {}
|
|
76
76
|
for event in events:
|
|
77
77
|
for item in event.get("payload") or ():
|
|
@@ -96,13 +96,14 @@ def parse_retry_after(value: str | None) -> int | None:
|
|
|
96
96
|
|
|
97
97
|
|
|
98
98
|
class Transport:
|
|
99
|
-
"""Owns the connection. One instance per client."""
|
|
99
|
+
"""Owns the connection pool. One instance per client."""
|
|
100
100
|
|
|
101
101
|
def __init__(self, config: ResolvedConfig, credentials: ApiKeyCredentials) -> None:
|
|
102
102
|
self._config = config
|
|
103
103
|
self._credentials = credentials
|
|
104
104
|
self._lock = threading.Lock()
|
|
105
|
-
self.
|
|
105
|
+
self._idle: list[http.client.HTTPConnection] = []
|
|
106
|
+
self._semaphore = threading.BoundedSemaphore(max(1, config.max_concurrent_requests))
|
|
106
107
|
self._closed = False
|
|
107
108
|
|
|
108
109
|
def set_config(self, config: ResolvedConfig) -> None:
|
|
@@ -112,37 +113,46 @@ class Transport:
|
|
|
112
113
|
or config.port != self._config.port
|
|
113
114
|
or config.scheme != self._config.scheme
|
|
114
115
|
)
|
|
116
|
+
resize = config.max_concurrent_requests != self._config.max_concurrent_requests
|
|
115
117
|
self._config = config
|
|
116
118
|
if reconnect:
|
|
117
|
-
self.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
119
|
+
self._drop_idle()
|
|
120
|
+
if resize:
|
|
121
|
+
self._semaphore = threading.BoundedSemaphore(max(1, config.max_concurrent_requests))
|
|
122
|
+
|
|
123
|
+
def _drop_idle(self) -> None:
|
|
124
|
+
for conn in self._idle:
|
|
125
|
+
_close_quietly(conn)
|
|
126
|
+
self._idle.clear()
|
|
127
|
+
|
|
128
|
+
def _checkout(self, config: ResolvedConfig) -> http.client.HTTPConnection:
|
|
129
|
+
with self._lock:
|
|
130
|
+
if self._closed:
|
|
131
|
+
raise IntemptApiError("client is closed")
|
|
132
|
+
if config.keep_alive and self._idle:
|
|
133
|
+
return self._idle.pop()
|
|
134
|
+
return self._new_connection(config)
|
|
135
|
+
|
|
136
|
+
@staticmethod
|
|
137
|
+
def _new_connection(config: ResolvedConfig) -> http.client.HTTPConnection:
|
|
130
138
|
cls = (
|
|
131
|
-
http.client.HTTPSConnection
|
|
132
|
-
if self._config.scheme == "https"
|
|
133
|
-
else http.client.HTTPConnection
|
|
139
|
+
http.client.HTTPSConnection if config.scheme == "https" else http.client.HTTPConnection
|
|
134
140
|
)
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
141
|
+
return cls(config.host, config.port, timeout=config.timeout)
|
|
142
|
+
|
|
143
|
+
def _checkin(self, conn: http.client.HTTPConnection) -> None:
|
|
144
|
+
with self._lock:
|
|
145
|
+
if self._closed:
|
|
146
|
+
_close_quietly(conn)
|
|
147
|
+
return
|
|
148
|
+
self._idle.append(conn)
|
|
139
149
|
|
|
140
150
|
def _connect(self, conn: http.client.HTTPConnection) -> None:
|
|
141
151
|
"""Connect before writing, so a failure here is known to be never-reached."""
|
|
142
152
|
try:
|
|
143
153
|
conn.connect()
|
|
144
154
|
except OSError as exc:
|
|
145
|
-
|
|
155
|
+
_close_quietly(conn)
|
|
146
156
|
raise IntemptApiError(
|
|
147
157
|
str(exc) or type(exc).__name__,
|
|
148
158
|
cause=exc,
|
|
@@ -171,10 +181,10 @@ class Transport:
|
|
|
171
181
|
if self._config.debug:
|
|
172
182
|
self._config.logger.debug("[intempt] POST %s", path)
|
|
173
183
|
|
|
174
|
-
with self.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
conn = self.
|
|
184
|
+
with self._semaphore:
|
|
185
|
+
with self._lock:
|
|
186
|
+
config = self._config
|
|
187
|
+
conn = self._checkout(config)
|
|
178
188
|
reused = conn.sock is not None
|
|
179
189
|
if not reused:
|
|
180
190
|
self._connect(conn)
|
|
@@ -188,24 +198,29 @@ class Transport:
|
|
|
188
198
|
# error comes after a write a live server may hold, and a resend duplicates.
|
|
189
199
|
if not reused:
|
|
190
200
|
raise
|
|
191
|
-
|
|
192
|
-
conn = self.
|
|
201
|
+
_close_quietly(conn)
|
|
202
|
+
conn = self._new_connection(config)
|
|
193
203
|
self._connect(conn)
|
|
194
204
|
response = self._exchange(conn, path, payload, headers)
|
|
195
205
|
raw = response.read().decode("utf-8", errors="replace")
|
|
196
206
|
status = response.status
|
|
197
207
|
retry_after = parse_retry_after(response.getheader("Retry-After"))
|
|
198
208
|
except socket.timeout as exc:
|
|
199
|
-
|
|
209
|
+
_close_quietly(conn)
|
|
200
210
|
raise IntemptApiError(
|
|
201
|
-
f"request timed out after {
|
|
211
|
+
f"request timed out after {config.timeout}s", cause=exc, timed_out=True
|
|
202
212
|
) from exc
|
|
203
213
|
except (http.client.HTTPException, OSError) as exc:
|
|
204
|
-
|
|
214
|
+
# A pooled connection the server already closed raises here on
|
|
215
|
+
# the next use. Drop it so the retry gets a fresh socket rather
|
|
216
|
+
# than failing again on the same dead one.
|
|
217
|
+
_close_quietly(conn)
|
|
205
218
|
raise IntemptApiError(str(exc) or type(exc).__name__, cause=exc) from exc
|
|
206
219
|
|
|
207
|
-
if
|
|
208
|
-
self.
|
|
220
|
+
if config.keep_alive:
|
|
221
|
+
self._checkin(conn)
|
|
222
|
+
else:
|
|
223
|
+
_close_quietly(conn)
|
|
209
224
|
|
|
210
225
|
if status < 200 or status >= 300:
|
|
211
226
|
raise IntemptApiError(
|
|
@@ -228,14 +243,28 @@ class Transport:
|
|
|
228
243
|
def close(self) -> None:
|
|
229
244
|
with self._lock:
|
|
230
245
|
self._closed = True
|
|
231
|
-
self.
|
|
246
|
+
self._drop_idle()
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _close_quietly(conn: http.client.HTTPConnection) -> None:
|
|
250
|
+
# Best effort: a socket the peer already dropped raises here, and there is
|
|
251
|
+
# nothing useful to do about it while tearing down.
|
|
252
|
+
with contextlib.suppress(Exception):
|
|
253
|
+
conn.close()
|
|
232
254
|
|
|
233
255
|
|
|
234
256
|
def _json_default(value: Any) -> Any:
|
|
235
257
|
import datetime as _dt
|
|
236
258
|
|
|
259
|
+
# Matches the Swift/JS/Node/RN wire format, so a property reads the same
|
|
260
|
+
# whichever SDK sent it. Naive means UTC, as in ensure_timestamp; a date is
|
|
261
|
+
# midnight UTC. datetime subclasses date, so it must be tested first.
|
|
237
262
|
if isinstance(value, _dt.datetime):
|
|
238
|
-
|
|
263
|
+
if value.tzinfo:
|
|
264
|
+
value = value.astimezone(_dt.timezone.utc).replace(tzinfo=None)
|
|
265
|
+
return value.isoformat(timespec="milliseconds") + "Z"
|
|
266
|
+
if isinstance(value, _dt.date):
|
|
267
|
+
return f"{value.isoformat()}T00:00:00.000Z"
|
|
239
268
|
if isinstance(value, (set, frozenset)):
|
|
240
269
|
return list(value)
|
|
241
270
|
raise TypeError(f"Object of type {type(value).__name__} is not JSON serialisable")
|
|
@@ -59,6 +59,9 @@ class RecordingServer:
|
|
|
59
59
|
def __init__(self) -> None:
|
|
60
60
|
self.requests: list[Captured] = []
|
|
61
61
|
self.replies: list[Reply] = []
|
|
62
|
+
self.in_flight = 0
|
|
63
|
+
self.max_in_flight = 0
|
|
64
|
+
self._epoch = 0
|
|
62
65
|
self._lock = threading.Lock()
|
|
63
66
|
outer = self
|
|
64
67
|
|
|
@@ -84,11 +87,22 @@ class RecordingServer:
|
|
|
84
87
|
)
|
|
85
88
|
)
|
|
86
89
|
reply = outer.replies.pop(0) if outer.replies else Reply()
|
|
90
|
+
epoch = outer._epoch
|
|
91
|
+
outer.in_flight += 1
|
|
92
|
+
outer.max_in_flight = max(outer.max_in_flight, outer.in_flight)
|
|
87
93
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
94
|
+
try:
|
|
95
|
+
if reply.delay_s:
|
|
96
|
+
import time as _t
|
|
97
|
+
|
|
98
|
+
_t.sleep(reply.delay_s)
|
|
99
|
+
finally:
|
|
100
|
+
with outer._lock:
|
|
101
|
+
# A handler that outlived a reset (a client that timed
|
|
102
|
+
# out and walked away) must not decrement the next
|
|
103
|
+
# test's gauge below zero.
|
|
104
|
+
if epoch == outer._epoch:
|
|
105
|
+
outer.in_flight -= 1
|
|
92
106
|
|
|
93
107
|
if reply.abort:
|
|
94
108
|
self._hang_up()
|
|
@@ -134,6 +148,9 @@ class RecordingServer:
|
|
|
134
148
|
with self._lock:
|
|
135
149
|
self.requests.clear()
|
|
136
150
|
self.replies.clear()
|
|
151
|
+
self.in_flight = 0
|
|
152
|
+
self.max_in_flight = 0
|
|
153
|
+
self._epoch += 1
|
|
137
154
|
|
|
138
155
|
def expect(self, *replies: Reply) -> None:
|
|
139
156
|
with self._lock:
|
|
@@ -5,6 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import base64
|
|
6
6
|
import datetime as _dt
|
|
7
7
|
import json
|
|
8
|
+
import uuid
|
|
8
9
|
|
|
9
10
|
import pytest
|
|
10
11
|
|
|
@@ -149,13 +150,16 @@ class TestEcommerce:
|
|
|
149
150
|
assert payload[0]["data"] == {"productId": "a", "quantity": 2}
|
|
150
151
|
assert payload[1]["data"] == {"productId": "b"}
|
|
151
152
|
|
|
152
|
-
def
|
|
153
|
-
"""
|
|
153
|
+
def test_ordered_gives_each_line_its_own_event_id(self, client, server):
|
|
154
|
+
"""INT-5415: the server kept one line of a multi-product order when they shared an id."""
|
|
154
155
|
client().ecommerce.ordered(
|
|
155
|
-
user_id="u1",
|
|
156
|
+
user_id="u1",
|
|
157
|
+
products=[{"product_id": "a"}, {"product_id": "b"}, {"product_id": "c"}],
|
|
156
158
|
)
|
|
157
159
|
payload = server.requests[0].body["track"][0]["payload"]
|
|
158
|
-
|
|
160
|
+
ids = [line["eventId"] for line in payload]
|
|
161
|
+
assert len(set(ids)) == 3
|
|
162
|
+
assert all(uuid.UUID(i) for i in ids)
|
|
159
163
|
|
|
160
164
|
def test_ordered_rejects_an_empty_product_list(self, client):
|
|
161
165
|
with pytest.raises(IntemptConfigError, match="non-empty sequence"):
|
|
@@ -274,6 +278,20 @@ class TestPrivacy:
|
|
|
274
278
|
c.close()
|
|
275
279
|
assert c.is_opted_in() is False
|
|
276
280
|
|
|
281
|
+
def test_has_opted_out_is_the_inverse_of_is_opted_in(self, client):
|
|
282
|
+
"""INT-5416: the public docs call has_opted_out()."""
|
|
283
|
+
c = client()
|
|
284
|
+
assert c.has_opted_out() is False
|
|
285
|
+
c.opt_out()
|
|
286
|
+
assert c.has_opted_out() is True
|
|
287
|
+
c.opt_in()
|
|
288
|
+
assert c.has_opted_out() is False
|
|
289
|
+
|
|
290
|
+
def test_has_opted_out_reports_true_after_close(self, client):
|
|
291
|
+
c = client()
|
|
292
|
+
c.close()
|
|
293
|
+
assert c.has_opted_out() is True
|
|
294
|
+
|
|
277
295
|
|
|
278
296
|
class TestLifecycle:
|
|
279
297
|
def test_calls_after_close_raise(self, client):
|
|
@@ -106,15 +106,17 @@ class TestListener:
|
|
|
106
106
|
|
|
107
107
|
assert [f.event_ids for f in heard.failures] == [("a",), ("b",)]
|
|
108
108
|
|
|
109
|
-
def
|
|
109
|
+
def test_commerce_lines_each_report_their_own_event_id(self, batched, server):
|
|
110
110
|
heard = Heard()
|
|
111
111
|
server.expect(Reply(status=400))
|
|
112
112
|
c = batched_with(batched, heard)
|
|
113
113
|
c.ecommerce.ordered(user_id="u1", products=[{"product_id": "p1"}, {"product_id": "p2"}])
|
|
114
114
|
c.flush()
|
|
115
115
|
|
|
116
|
+
sent = [line["eventId"] for line in server.requests[0].body["track"][0]["payload"]]
|
|
116
117
|
assert heard.failures[0].event_count == 1
|
|
117
|
-
assert
|
|
118
|
+
assert heard.failures[0].event_ids == tuple(sent)
|
|
119
|
+
assert len(set(sent)) == 2
|
|
118
120
|
|
|
119
121
|
def test_a_listener_that_raises_is_caught_and_logged(self, batched, server, logger):
|
|
120
122
|
def boom(_failure):
|
|
@@ -104,7 +104,7 @@ class TestFailures:
|
|
|
104
104
|
c = client()
|
|
105
105
|
c.track("a", user_id="u1")
|
|
106
106
|
# Close the pooled connection underneath the client.
|
|
107
|
-
c._transport.
|
|
107
|
+
c._transport._idle[0].close()
|
|
108
108
|
c.track("b", user_id="u1")
|
|
109
109
|
assert len(server.requests) == 2
|
|
110
110
|
|
|
@@ -181,9 +181,43 @@ class TestSerialisation:
|
|
|
181
181
|
def test_serialises_a_datetime_in_properties(self, client, server):
|
|
182
182
|
import datetime as _dt
|
|
183
183
|
|
|
184
|
-
when = _dt.datetime(2026, 1, 1, tzinfo=_dt.timezone.utc)
|
|
184
|
+
when = _dt.datetime(2026, 1, 1, 9, 30, 5, 123456, tzinfo=_dt.timezone.utc)
|
|
185
185
|
client().track("purchase", user_id="u1", properties={"at": when})
|
|
186
|
-
assert
|
|
186
|
+
assert (
|
|
187
|
+
server.requests[0].body["track"][0]["payload"][0]["data"]["at"]
|
|
188
|
+
== "2026-01-01T09:30:05.123Z"
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
def test_serialises_a_datetime_in_attributes_as_iso_utc(self, client, server):
|
|
192
|
+
"""INT-5418: Swift/JS/Node/RN send ISO-8601 UTC with milliseconds."""
|
|
193
|
+
import datetime as _dt
|
|
194
|
+
|
|
195
|
+
when = _dt.datetime(2026, 1, 1, 12, 0, tzinfo=_dt.timezone(_dt.timedelta(hours=4)))
|
|
196
|
+
client().track(
|
|
197
|
+
"purchase",
|
|
198
|
+
user_id="u1",
|
|
199
|
+
user_attributes={"signed_up": when},
|
|
200
|
+
account_attributes={"renewal": _dt.date(2026, 3, 1)},
|
|
201
|
+
)
|
|
202
|
+
item = server.requests[0].body["track"][0]["payload"][0]
|
|
203
|
+
assert item["userAttributes"]["signed_up"] == "2026-01-01T08:00:00.000Z"
|
|
204
|
+
assert item["accountAttributes"]["renewal"] == "2026-03-01T00:00:00.000Z"
|
|
205
|
+
|
|
206
|
+
def test_serialises_a_naive_datetime_as_utc(self, client, server):
|
|
207
|
+
import datetime as _dt
|
|
208
|
+
|
|
209
|
+
client().track("purchase", user_id="u1", properties={"at": _dt.datetime(2026, 1, 1, 9, 30)})
|
|
210
|
+
assert (
|
|
211
|
+
server.requests[0].body["track"][0]["payload"][0]["data"]["at"]
|
|
212
|
+
== "2026-01-01T09:30:00.000Z"
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
def test_keeps_the_event_timestamp_in_epoch_ms(self, client, server):
|
|
216
|
+
import datetime as _dt
|
|
217
|
+
|
|
218
|
+
when = _dt.datetime(2026, 1, 1, tzinfo=_dt.timezone.utc)
|
|
219
|
+
client().track("purchase", user_id="u1", timestamp=when)
|
|
220
|
+
assert server.requests[0].body["track"][0]["payload"][0]["timestamp"] == int(
|
|
187
221
|
when.timestamp() * 1000
|
|
188
222
|
)
|
|
189
223
|
|
|
@@ -213,6 +247,30 @@ class TestConcurrency:
|
|
|
213
247
|
# Three workers on a shared cursor means at most three sockets.
|
|
214
248
|
assert len({r.socket_id for r in server.requests}) <= 3
|
|
215
249
|
|
|
250
|
+
def test_requests_actually_run_in_parallel(self, client, server):
|
|
251
|
+
server.expect(*[Reply(delay_s=0.2) for _ in range(3)])
|
|
252
|
+
c = client(max_request_events=1, max_concurrent_requests=3)
|
|
253
|
+
c.track_batch([{"event": f"e{i}", "user_id": "u1"} for i in range(3)])
|
|
254
|
+
|
|
255
|
+
assert len(server.requests) == 3
|
|
256
|
+
assert server.max_in_flight == 3
|
|
257
|
+
|
|
258
|
+
def test_concurrency_is_bounded_by_the_semaphore(self, client, server):
|
|
259
|
+
server.expect(*[Reply(delay_s=0.1) for _ in range(6)])
|
|
260
|
+
c = client(max_request_events=1, max_concurrent_requests=2)
|
|
261
|
+
c.track_batch([{"event": f"e{i}", "user_id": "u1"} for i in range(6)])
|
|
262
|
+
|
|
263
|
+
assert len(server.requests) == 6
|
|
264
|
+
assert server.max_in_flight == 2
|
|
265
|
+
|
|
266
|
+
def test_set_config_resizes_the_concurrency_bound(self, client, server):
|
|
267
|
+
server.expect(*[Reply(delay_s=0.2) for _ in range(3)])
|
|
268
|
+
c = client(max_request_events=1, max_concurrent_requests=1)
|
|
269
|
+
c.set_config(max_concurrent_requests=3)
|
|
270
|
+
c.track_batch([{"event": f"e{i}", "user_id": "u1"} for i in range(3)])
|
|
271
|
+
|
|
272
|
+
assert server.max_in_flight == 3
|
|
273
|
+
|
|
216
274
|
def test_a_failure_in_one_chunk_surfaces(self, client, server):
|
|
217
275
|
server.expect(Reply(), Reply(status=500, body="boom"), Reply())
|
|
218
276
|
c = client(max_request_events=1, max_concurrent_requests=3)
|
|
@@ -247,9 +305,9 @@ class TestTransportUnit:
|
|
|
247
305
|
)
|
|
248
306
|
transport = Transport(config, ApiKeyCredentials(API_KEY))
|
|
249
307
|
transport.post(config.project_path("/track"), {"track": []})
|
|
250
|
-
first = transport.
|
|
308
|
+
first = transport._idle[0]
|
|
251
309
|
transport.set_config(config)
|
|
252
|
-
assert transport.
|
|
310
|
+
assert transport._idle[0] is first
|
|
253
311
|
transport.close()
|
|
254
312
|
|
|
255
313
|
def test_close_is_idempotent(self, server):
|
|
@@ -307,7 +365,7 @@ class TestTransportErrorShapes:
|
|
|
307
365
|
assert caught.value.cause is refused
|
|
308
366
|
assert caught.value.__cause__ is refused
|
|
309
367
|
assert str(caught.value) == str(refused)
|
|
310
|
-
assert transport.
|
|
368
|
+
assert transport._idle == []
|
|
311
369
|
|
|
312
370
|
def test_a_connect_timeout_is_never_reached_and_timed_out(self, server, monkeypatch):
|
|
313
371
|
self.refuse_with(monkeypatch, socket.timeout("timed out"))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|