@@ -15,66 +15,277 @@ import (
1515 "github.com/urnetwork/server"
1616)
1717
18- func TestTrySendPooledReceiveRefusesFullQueueWithoutWaiting (t * testing.T ) {
18+ func TestSendPooledReceiveUnreliableRefusesWithOwnership (t * testing.T ) {
1919 destination := make (chan []byte , 1 )
20- queued := clientconnect .MessagePoolGet (1 )
21- destination <- queued
20+ destination <- clientconnect .MessagePoolGet (1 )
2221 defer func () { clientconnect .MessagePoolReturn (<- destination ) }()
22+ message := clientconnect .MessagePoolGet (173 )
23+ witness := clientconnect .MessagePoolShareReadOnly (message )
24+ result := sendPooledReceive (
25+ make (chan struct {}),
26+ nil ,
27+ destination ,
28+ message ,
29+ clientconnect .CarrierReliabilityUnreliable ,
30+ )
31+ if result != pooledMessageSendDropped {
32+ t .Fatalf ("full datagram queue result=%d, want dropped" , result )
33+ }
34+ if ! clientconnect .MessagePoolReturn (witness ) {
35+ t .Fatal ("datagram refusal retained pooled bytes" )
36+ }
37+ }
2338
24- result := pooledMessageSendDelivered
39+ func TestSendPooledReceiveReliableWaitsThenDelivers (t * testing.T ) {
40+ destination := make (chan []byte , 1 )
41+ destination <- clientconnect .MessagePoolGet (1 )
42+ message := clientconnect .MessagePoolGet (181 )
43+ witness := clientconnect .MessagePoolShareReadOnly (message )
44+ result := pooledMessageSendDropped
2545 done := make (chan struct {})
46+ waiting := make (chan struct {})
2647 go func () {
2748 defer close (done )
28- result = trySendPooledReceive (
49+ result = sendPooledReceive (
2950 make (chan struct {}),
3051 nil ,
3152 destination ,
32- clientconnect .MessagePoolGet (173 ),
53+ message ,
54+ clientconnect .CarrierReliabilityReliable ,
55+ func () { close (waiting ) },
3356 )
3457 }()
58+ <- waiting
59+ select {
60+ case <- done :
61+ t .Fatal ("reliable handoff returned while its queue was full" )
62+ default :
63+ }
64+ clientconnect .MessagePoolReturn (<- destination )
3565 select {
3666 case <- done :
3767 case <- time .After (time .Second ):
38- t .Fatal ("receive-side queue offer waited for capacity" )
68+ t .Fatal ("reliable handoff did not resume when capacity opened " )
3969 }
40- if result != pooledMessageSendDropped {
41- t .Fatalf ("full receive queue result=%d, want dropped" , result )
70+ if result != pooledMessageSendDelivered {
71+ t .Fatalf ("resumed reliable handoff result=%d, want delivered" , result )
72+ }
73+ clientconnect .MessagePoolReturn (<- destination )
74+ if ! clientconnect .MessagePoolReturn (witness ) {
75+ t .Fatal ("delivered reliable message has an unexpected pooled owner" )
4276 }
4377}
4478
45- func TestResidentTransportTrySendMessageRefusesWithoutWaiting (t * testing.T ) {
79+ func TestSendPooledReceiveReliableCancellationReturnsOwnership (t * testing.T ) {
80+ destination := make (chan []byte , 1 )
81+ destination <- clientconnect .MessagePoolGet (1 )
82+ defer func () { clientconnect .MessagePoolReturn (<- destination ) }()
4683 ctxDone := make (chan struct {})
84+ message := clientconnect .MessagePoolGet (191 )
85+ witness := clientconnect .MessagePoolShareReadOnly (message )
86+ result := pooledMessageSendDelivered
87+ done := make (chan struct {})
88+ waiting := make (chan struct {})
89+ go func () {
90+ defer close (done )
91+ result = sendPooledReceive (
92+ ctxDone ,
93+ nil ,
94+ destination ,
95+ message ,
96+ clientconnect .CarrierReliabilityReliable ,
97+ func () { close (waiting ) },
98+ )
99+ }()
100+ <- waiting
101+ select {
102+ case <- done :
103+ t .Fatal ("reliable handoff returned before cancellation" )
104+ default :
105+ }
106+ close (ctxDone )
107+ select {
108+ case <- done :
109+ case <- time .After (time .Second ):
110+ t .Fatal ("reliable handoff ignored cancellation" )
111+ }
112+ if result != pooledMessageSendDone {
113+ t .Fatalf ("canceled reliable handoff result=%d, want done" , result )
114+ }
115+ if ! clientconnect .MessagePoolReturn (witness ) {
116+ t .Fatal ("canceled reliable handoff retained pooled bytes" )
117+ }
118+ }
119+
120+ func benchmarkSendPooledReceiveReady (
121+ benchmark * testing.B ,
122+ reliability clientconnect.CarrierReliability ,
123+ ) {
124+ destination := make (chan []byte , 1 )
125+ message := []byte {1 }
126+ benchmark .ReportAllocs ()
127+ benchmark .ResetTimer ()
128+ for range benchmark .N {
129+ result := sendPooledReceive (nil , nil , destination , message , reliability )
130+ if result != pooledMessageSendDelivered {
131+ benchmark .Fatalf ("ready handoff result=%d, want delivered" , result )
132+ }
133+ <- destination
134+ }
135+ }
136+
137+ func BenchmarkSendPooledReceiveReadyReliable (benchmark * testing.B ) {
138+ benchmarkSendPooledReceiveReady (benchmark , clientconnect .CarrierReliabilityReliable )
139+ }
140+
141+ func BenchmarkSendPooledReceiveReadyUnreliable (benchmark * testing.B ) {
142+ benchmarkSendPooledReceiveReady (benchmark , clientconnect .CarrierReliabilityUnreliable )
143+ }
144+
145+ func benchmarkResidentTransportSendReceivedReady (
146+ benchmark * testing.B ,
147+ reliability clientconnect.CarrierReliability ,
148+ ) {
149+ destination := make (chan []byte , 1 )
150+ transport := & ResidentTransport {ctx : context .Background (), send : destination }
151+ message := []byte {1 }
152+ benchmark .ReportAllocs ()
153+ benchmark .ResetTimer ()
154+ for range benchmark .N {
155+ result := transport .sendReceivedMessage (nil , message , reliability )
156+ if result != pooledMessageSendDelivered {
157+ benchmark .Fatalf ("ready resident handoff result=%d, want delivered" , result )
158+ }
159+ <- destination
160+ }
161+ }
162+
163+ func BenchmarkResidentTransportSendReceivedReadyReliable (benchmark * testing.B ) {
164+ benchmarkResidentTransportSendReceivedReady (
165+ benchmark ,
166+ clientconnect .CarrierReliabilityReliable ,
167+ )
168+ }
169+
170+ func BenchmarkResidentTransportSendReceivedReadyUnreliable (benchmark * testing.B ) {
171+ benchmarkResidentTransportSendReceivedReady (
172+ benchmark ,
173+ clientconnect .CarrierReliabilityUnreliable ,
174+ )
175+ }
176+
177+ // Reproduces the internal-hop root cause with explicit sequence markers. A
178+ // full reliable exchange queue must preserve 0,1 order instead of dropping 1
179+ // and letting a later frame pin Transfer recovery behind an artificial gap.
180+ func TestReliableExchangeQueueSaturationPreservesFramedOrder (t * testing.T ) {
181+ destination := make (chan []byte , 1 )
182+ first := clientconnect .MessagePoolGet (8 )
183+ first [0 ] = 0
184+ second := clientconnect .MessagePoolGet (8 )
185+ second [0 ] = 1
186+ destination <- first
187+ result := pooledMessageSendDropped
188+ done := make (chan struct {})
189+ waiting := make (chan struct {})
190+ go func () {
191+ defer close (done )
192+ result = sendPooledReceive (
193+ make (chan struct {}),
194+ nil ,
195+ destination ,
196+ second ,
197+ clientconnect .CarrierReliabilityReliable ,
198+ func () { close (waiting ) },
199+ )
200+ }()
201+ <- waiting
202+ select {
203+ case <- done :
204+ t .Fatal ("second reliable frame bypassed full queue" )
205+ default :
206+ }
207+ got := <- destination
208+ if got [0 ] != 0 {
209+ t .Fatalf ("first framed marker=%d want=0" , got [0 ])
210+ }
211+ clientconnect .MessagePoolReturn (got )
212+ select {
213+ case <- done :
214+ case <- time .After (time .Second ):
215+ t .Fatal ("second reliable frame did not resume" )
216+ }
217+ if result != pooledMessageSendDelivered {
218+ t .Fatalf ("second framed result=%d want delivered" , result )
219+ }
220+ got = <- destination
221+ if got [0 ] != 1 {
222+ t .Fatalf ("second framed marker=%d want=1" , got [0 ])
223+ }
224+ clientconnect .MessagePoolReturn (got )
225+ }
226+
227+ func TestExchangeGenerationRetiresAfterAnyUndeliveredFrame (t * testing.T ) {
228+ if ! pooledMessageSendKeepsGeneration (pooledMessageSendDelivered ) {
229+ t .Fatal ("delivered exchange frame retired its generation" )
230+ }
231+ for _ , result := range []pooledMessageSendResult {
232+ pooledMessageSendDropped ,
233+ pooledMessageSendDone ,
234+ } {
235+ if pooledMessageSendKeepsGeneration (result ) {
236+ t .Fatalf ("undelivered exchange result=%d kept a gapped generation" , result )
237+ }
238+ }
239+ }
240+
241+ func TestResidentTransportReceiveAdmissionUsesExactLane (t * testing.T ) {
47242 ctx , cancel := context .WithCancel (context .Background ())
48- defer cancel ()
49243 destination := make (chan []byte , 1 )
50- queued := clientconnect .MessagePoolGet (1 )
51- destination <- queued
244+ destination <- clientconnect .MessagePoolGet (1 )
52245 defer func () { clientconnect .MessagePoolReturn (<- destination ) }()
246+ transport := & ResidentTransport {ctx : ctx , send : destination }
247+ waiting := make (chan struct {})
248+ transport .beforeReliableReceiveWaitForTest = func () { close (waiting ) }
53249
54- transport := & ResidentTransport {
55- ctx : ctx ,
56- send : destination ,
250+ unreliable := clientconnect .MessagePoolGet (197 )
251+ if result := transport .sendReceivedMessage (
252+ make (chan struct {}),
253+ unreliable ,
254+ clientconnect .CarrierReliabilityUnreliable ,
255+ ); result != pooledMessageSendDropped {
256+ t .Fatalf ("full resident DATAGRAM lane result=%d, want dropped" , result )
57257 }
258+
259+ reliable := clientconnect .MessagePoolGet (199 )
260+ witness := clientconnect .MessagePoolShareReadOnly (reliable )
58261 result := pooledMessageSendDelivered
59262 done := make (chan struct {})
60263 go func () {
61264 defer close (done )
62- result = transport .trySendMessage (
63- ctxDone ,
64- clientconnect .MessagePoolGet (197 ),
265+ result = transport .sendReceivedMessage (
266+ make (chan struct {}),
267+ reliable ,
268+ clientconnect .CarrierReliabilityReliable ,
65269 )
66270 }()
271+ <- waiting
272+ select {
273+ case <- done :
274+ t .Fatal ("resident reliable stream did not backpressure" )
275+ default :
276+ }
277+ cancel ()
67278 select {
68279 case <- done :
69280 case <- time .After (time .Second ):
70- t .Fatal ("resident transport receive handoff waited for capacity " )
281+ t .Fatal ("resident reliable stream ignored generation cancellation " )
71282 }
72- if result != pooledMessageSendDropped {
73- t .Fatalf ( "full resident transport result=%d, want dropped" , result )
283+ if result != pooledMessageSendDone || ! clientconnect . MessagePoolReturn ( witness ) {
284+ t .Fatal ( " resident cancellation did not return reliable message ownership" )
74285 }
75286}
76287
77- func TestProductionSocketReadersUseZeroWaitQueueAdmission (t * testing.T ) {
288+ func TestProductionSocketReadersDeclareExactReceiveLanes (t * testing.T ) {
78289 checks := []struct {
79290 path string
80291 required map [string ]int
@@ -83,21 +294,22 @@ func TestProductionSocketReadersUseZeroWaitQueueAdmission(t *testing.T) {
83294 {
84295 path : "transport.go" ,
85296 required : map [string ]int {
86- "residentTransport.trySendMessage(" : 2 ,
297+ "residentTransport.sendReceivedMessage(" : 2 ,
298+ "connect.CarrierReliabilityUnreliable" : 1 ,
87299 },
88300 forbiddenSnippets : []string {
89- "residentTransport.sendMessage (" ,
301+ "residentTransport.trySendMessage (" ,
90302 },
91303 },
92304 {
93305 path : "resident.go" ,
94306 required : map [string ]int {
95- "trySendPooledReceive(" : 6 , // declaration plus five receive boundaries
307+ "sendPooledReceive(" : 6 , // declaration plus five receive boundaries
308+ "pooledMessageSendKeepsGeneration(" : 3 , // declaration plus transport/forward writers
309+ "ReceiveReliability:" : 1 , // resident's framed TCP receive route
96310 },
97311 forbiddenSnippets : []string {
98- "case receive <- message:" ,
99- "case forward <- message:" ,
100- "case self.receive <- message:" ,
312+ "trySendPooledReceive(" ,
101313 },
102314 },
103315 }
@@ -120,7 +332,7 @@ func TestProductionSocketReadersUseZeroWaitQueueAdmission(t *testing.T) {
120332 }
121333}
122334
123- func TestResidentForwardCallbackRefusesFullIngressWithoutWaiting (t * testing.T ) {
335+ func TestResidentForwardCallbackRetiresFullIngressWithoutWaiting (t * testing.T ) {
124336 ctx , cancel := context .WithCancel (context .Background ())
125337 defer cancel ()
126338 clientId := server .NewId ()
@@ -156,6 +368,11 @@ func TestResidentForwardCallbackRefusesFullIngressWithoutWaiting(t *testing.T) {
156368 case <- time .After (time .Second ):
157369 t .Fatal ("resident forward callback waited for ingress capacity" )
158370 }
371+ select {
372+ case <- ctx .Done ():
373+ default :
374+ t .Fatal ("reliable forward refusal did not retire the resident generation" )
375+ }
159376 clientconnect .MessagePoolReturn (message )
160377 if ! clientconnect .MessagePoolReturn (witness ) {
161378 t .Fatal ("resident forward refusal retained callback-owned bytes" )
@@ -273,6 +490,7 @@ func TestResidentClientCallbacksContainOnlyZeroWaitIngressWork(t *testing.T) {
273490 "MessagePoolShareReadOnly(transferFrameBytes)" ,
274491 "case self.forwardIngress[shardIndex] <- message:" ,
275492 "default:" ,
493+ "self.cancel()" ,
276494 },
277495 forbidden : []string {
278496 "stateLock." ,
0 commit comments