react-native-nitro-sse 2.0.0 → 2.2.0
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.
- package/NitroSse.podspec +1 -1
- package/README.md +4 -0
- package/android/build.gradle +1 -0
- package/android/src/main/java/com/margelo/nitro/nitrosse/NitroSse.kt +226 -36
- package/ios/NitroSse.swift +147 -29
- package/lib/module/SseInterface.js +2 -0
- package/lib/typescript/src/SseInterface.d.ts +17 -1
- package/lib/typescript/src/SseInterface.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_SseEvent_.hpp +2 -0
- package/nitrogen/generated/android/c++/JHybridNitroSseSpec.cpp +2 -0
- package/nitrogen/generated/android/c++/JSseConfig.hpp +9 -1
- package/nitrogen/generated/android/c++/JSseEvent.hpp +7 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseConfig.kt +10 -4
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrosse/SseEvent.kt +6 -3
- package/nitrogen/generated/ios/NitroSse+autolinking.rb +2 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Bridge.hpp +16 -0
- package/nitrogen/generated/ios/NitroSse-Swift-Cxx-Umbrella.hpp +1 -0
- package/nitrogen/generated/ios/c++/HybridNitroSseSpecSwift.hpp +1 -0
- package/nitrogen/generated/ios/swift/SseConfig.swift +37 -1
- package/nitrogen/generated/ios/swift/SseEvent.swift +19 -1
- package/nitrogen/generated/shared/c++/SseConfig.hpp +9 -1
- package/nitrogen/generated/shared/c++/SseEvent.hpp +6 -1
- package/package.json +11 -6
- package/src/SseInterface.ts +17 -1
- package/android/src/test/java/com/margelo/nitro/nitrosse/HeartbeatScannerTest.kt +0 -98
- package/android/src/test/java/com/margelo/nitro/nitrosse/NitroSseLogicTest.kt +0 -238
- package/ios/Tests/NitroSseTests.swift +0 -242
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
import XCTest
|
|
2
|
-
@testable import NitroSse
|
|
3
|
-
|
|
4
|
-
class NitroSseTests: XCTestCase {
|
|
5
|
-
|
|
6
|
-
// Core logic extraction for unit testing
|
|
7
|
-
|
|
8
|
-
func testBackoffWithJitterCalculation() {
|
|
9
|
-
let retryInterval: TimeInterval = 1.0
|
|
10
|
-
let maxRetryInterval: TimeInterval = 30.0
|
|
11
|
-
let jitterFactor: Double = 0.5
|
|
12
|
-
|
|
13
|
-
func calculateDelay(counter: Int, jitter: Double) -> TimeInterval {
|
|
14
|
-
let exponent = Double(counter)
|
|
15
|
-
let base = min(retryInterval * pow(2.0, exponent), maxRetryInterval)
|
|
16
|
-
// Logic from NitroSse.swift: base * (1.0 - jitterFactor + Double.random(in: 0...(2 * jitterFactor)))
|
|
17
|
-
return base * (1.0 - jitterFactor + (jitter * 2 * jitterFactor))
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Test range for counter 0 (Base 1.0s)
|
|
21
|
-
// Min: 1.0 * (1.0 - 0.5 + 0) = 0.5s
|
|
22
|
-
// Max: 1.0 * (1.0 - 0.5 + 1.0) = 1.5s
|
|
23
|
-
XCTAssertEqual(calculateDelay(counter: 0, jitter: 0.0), 0.5)
|
|
24
|
-
XCTAssertEqual(calculateDelay(counter: 0, jitter: 1.0), 1.5)
|
|
25
|
-
|
|
26
|
-
// Test range for counter 5 (Base 32.0s -> Capped 30.0s)
|
|
27
|
-
// Min: 30.0 * 0.5 = 15.0s
|
|
28
|
-
// Max: 30.0 * 1.5 = 45.0s
|
|
29
|
-
XCTAssertEqual(calculateDelay(counter: 5, jitter: 0.0), 15.0)
|
|
30
|
-
XCTAssertEqual(calculateDelay(counter: 5, jitter: 1.0), 45.0)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
func testZeroLossBuffering() {
|
|
34
|
-
var buffer: [String] = []
|
|
35
|
-
var flushCount = 0
|
|
36
|
-
let capacity = 5
|
|
37
|
-
|
|
38
|
-
func pushToBuffer(_ item: String) {
|
|
39
|
-
buffer.append(item)
|
|
40
|
-
if buffer.count >= capacity {
|
|
41
|
-
// Simulate flush logic from NitroSse.swift
|
|
42
|
-
flushCount += 1
|
|
43
|
-
buffer.removeAll()
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Fill buffer to capacity
|
|
48
|
-
for i in 1...5 {
|
|
49
|
-
pushToBuffer("event-\(i)")
|
|
50
|
-
}
|
|
51
|
-
XCTAssertEqual(flushCount, 1, "Should flush when capacity reached")
|
|
52
|
-
XCTAssertEqual(buffer.count, 0, "Buffer should be empty after flush")
|
|
53
|
-
|
|
54
|
-
// Check it doesn't drop items
|
|
55
|
-
for i in 1...3 {
|
|
56
|
-
pushToBuffer("event-\(i)")
|
|
57
|
-
}
|
|
58
|
-
XCTAssertEqual(buffer.count, 3)
|
|
59
|
-
XCTAssertEqual(flushCount, 1)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
func testMaxReconnectAttemptsStop() {
|
|
63
|
-
let maxAttempts = 3
|
|
64
|
-
var currentReconnectAttempts = 0
|
|
65
|
-
var isRunning = true
|
|
66
|
-
|
|
67
|
-
func onFailure() {
|
|
68
|
-
if currentReconnectAttempts >= maxAttempts {
|
|
69
|
-
isRunning = false
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
currentReconnectAttempts += 1
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
for _ in 1...3 {
|
|
76
|
-
onFailure()
|
|
77
|
-
}
|
|
78
|
-
XCTAssertTrue(isRunning, "Should still be running at attempt 3")
|
|
79
|
-
XCTAssertEqual(currentReconnectAttempts, 3)
|
|
80
|
-
|
|
81
|
-
onFailure() // 4th call, current (3) >= max (3)
|
|
82
|
-
XCTAssertFalse(isRunning, "Should stop after 3 attempts")
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
func testAuthErrorLogic() {
|
|
86
|
-
func shouldRetry(statusCode: Int, hasInterceptor: Bool, consecutiveAuthErrors: Int) -> Bool {
|
|
87
|
-
let maxAuthRetries = 3
|
|
88
|
-
if statusCode == 401 || statusCode == 403 {
|
|
89
|
-
if !hasInterceptor { return false }
|
|
90
|
-
if consecutiveAuthErrors >= maxAuthRetries { return false }
|
|
91
|
-
return true
|
|
92
|
-
}
|
|
93
|
-
return false
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Case 1: 401 without interceptor -> Fail
|
|
97
|
-
XCTAssertFalse(shouldRetry(statusCode: 401, hasInterceptor: false, consecutiveAuthErrors: 0))
|
|
98
|
-
|
|
99
|
-
// Case 2: 401 with interceptor -> Retry
|
|
100
|
-
XCTAssertTrue(shouldRetry(statusCode: 401, hasInterceptor: true, consecutiveAuthErrors: 0))
|
|
101
|
-
|
|
102
|
-
// Case 3: 401 reaching limit -> Fail
|
|
103
|
-
XCTAssertFalse(shouldRetry(statusCode: 401, hasInterceptor: true, consecutiveAuthErrors: 3))
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
func testFatalErrorCodeStop() {
|
|
107
|
-
func isFatal(statusCode: Int) -> Bool {
|
|
108
|
-
return statusCode == 400 || statusCode == 204
|
|
109
|
-
}
|
|
110
|
-
XCTAssertTrue(isFatal(statusCode: 400))
|
|
111
|
-
XCTAssertTrue(isFatal(statusCode: 204))
|
|
112
|
-
XCTAssertFalse(isFatal(statusCode: 500))
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
func testRetryAfterDateParsing() {
|
|
116
|
-
func extractRetryAfter(headerValue: String?) -> TimeInterval? {
|
|
117
|
-
guard let retryAfterHeader = headerValue else { return nil }
|
|
118
|
-
if let seconds = Double(retryAfterHeader) {
|
|
119
|
-
return seconds
|
|
120
|
-
}
|
|
121
|
-
let formatter = DateFormatter()
|
|
122
|
-
formatter.locale = Locale(identifier: "en_US_POSIX")
|
|
123
|
-
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
|
|
124
|
-
if let date = formatter.date(from: retryAfterHeader) {
|
|
125
|
-
// For testability, we mock a "now" date
|
|
126
|
-
let mockNow = formatter.date(from: "Wed, 01 Jan 2026 00:00:00 GMT")!
|
|
127
|
-
let diff = date.timeIntervalSince(mockNow)
|
|
128
|
-
return diff > 0 ? diff : nil
|
|
129
|
-
}
|
|
130
|
-
return nil
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// 5 seconds
|
|
134
|
-
XCTAssertEqual(extractRetryAfter(headerValue: "5"), 5.0)
|
|
135
|
-
|
|
136
|
-
// Date exactly 1 hour in future
|
|
137
|
-
let futureDate = "Wed, 01 Jan 2026 01:00:00 GMT"
|
|
138
|
-
XCTAssertEqual(extractRetryAfter(headerValue: futureDate), 3600.0)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
func testConcurrentStartStopVersioning() {
|
|
142
|
-
var connectionAttemptVersion = 0
|
|
143
|
-
var executeCount = 0
|
|
144
|
-
|
|
145
|
-
func start() -> Int {
|
|
146
|
-
return connectionAttemptVersion // Captures current version
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
func stop() {
|
|
150
|
-
connectionAttemptVersion += 1 // Invalidates current version
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// 1. User starts
|
|
154
|
-
let version1 = start()
|
|
155
|
-
|
|
156
|
-
// 2. User stops unexpectedly (e.g. Navigation back)
|
|
157
|
-
stop()
|
|
158
|
-
|
|
159
|
-
// 3. Logic tries to finish connection with version 1
|
|
160
|
-
func finishConnection(version: Int) {
|
|
161
|
-
if version == connectionAttemptVersion {
|
|
162
|
-
executeCount += 1
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
finishConnection(version: version1)
|
|
167
|
-
XCTAssertEqual(executeCount, 0, "Should have ignored execution because version changed")
|
|
168
|
-
|
|
169
|
-
let version2 = start()
|
|
170
|
-
finishConnection(version: version2)
|
|
171
|
-
XCTAssertEqual(executeCount, 1, "Should execute for fresh version")
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
func testStartIsIdempotent() {
|
|
175
|
-
var isRunning = false
|
|
176
|
-
var startCount = 0
|
|
177
|
-
|
|
178
|
-
func start() {
|
|
179
|
-
guard !isRunning else { return }
|
|
180
|
-
isRunning = true
|
|
181
|
-
startCount += 1
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
start()
|
|
185
|
-
start()
|
|
186
|
-
XCTAssertEqual(startCount, 1, "start() should be idempotent")
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
func testHibernationLogic() {
|
|
190
|
-
var isRunning = true
|
|
191
|
-
var wasRunningBeforeHibernation = false
|
|
192
|
-
|
|
193
|
-
func hibernate() {
|
|
194
|
-
if isRunning {
|
|
195
|
-
wasRunningBeforeHibernation = true
|
|
196
|
-
isRunning = false
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
func resume() {
|
|
201
|
-
if wasRunningBeforeHibernation {
|
|
202
|
-
isRunning = true
|
|
203
|
-
wasRunningBeforeHibernation = false
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// App goes to background
|
|
208
|
-
hibernate()
|
|
209
|
-
XCTAssertFalse(isRunning)
|
|
210
|
-
XCTAssertTrue(wasRunningBeforeHibernation)
|
|
211
|
-
|
|
212
|
-
// App returns to foreground
|
|
213
|
-
resume()
|
|
214
|
-
XCTAssertTrue(isRunning)
|
|
215
|
-
XCTAssertFalse(wasRunningBeforeHibernation)
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
func testStatsPersistenceAcrossReconnections() {
|
|
219
|
-
var totalBytes: Double = 0
|
|
220
|
-
var reconnectCount = 0
|
|
221
|
-
|
|
222
|
-
func simulateConnectionCycle(bytes: Double) {
|
|
223
|
-
reconnectCount += 1
|
|
224
|
-
totalBytes += bytes
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
simulateConnectionCycle(bytes: 100)
|
|
228
|
-
simulateConnectionCycle(bytes: 250)
|
|
229
|
-
|
|
230
|
-
XCTAssertEqual(reconnectCount, 2)
|
|
231
|
-
XCTAssertEqual(totalBytes, 350, "Bytes should be cumulative")
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
func testHeartbeatSizeReporting() {
|
|
235
|
-
func getHeartbeatSize(comment: String) -> Double {
|
|
236
|
-
return Double(comment.utf8.count)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
XCTAssertEqual(getHeartbeatSize(comment: ":keep-alive"), 11.0)
|
|
240
|
-
XCTAssertEqual(getHeartbeatSize(comment: ":"), 1.0)
|
|
241
|
-
}
|
|
242
|
-
}
|