-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathe2e_test.go
More file actions
613 lines (498 loc) · 17.2 KB
/
Copy pathe2e_test.go
File metadata and controls
613 lines (498 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package serverconn
import (
"context"
"fmt"
"testing"
"time"
"github.com/lightninglabs/wavelength/baselib/actor"
mailboxpb "github.com/lightninglabs/wavelength/mailbox/pb"
mailboxrpc "github.com/lightninglabs/wavelength/mailbox/rpc"
"github.com/lightninglabs/wavelength/serverconn/hellotestpb"
fn "github.com/lightningnetwork/lnd/fn/v2"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
// helloStartedMsg is the actor-local representation of a server-pushed
// HelloStartedEvent. It implements InboundActorMessage so it can be
// registered with NewEventRoute for automatic FromProto dispatch.
type helloStartedMsg struct {
actor.BaseMessage
// SessionID identifies the greeting session.
SessionID string
}
// MessageType returns a human-readable type name for logging.
func (m *helloStartedMsg) MessageType() string { return "HelloStartedMsg" }
// FromProto populates the message from a deserialized HelloStartedEvent.
func (m *helloStartedMsg) FromProto(p proto.Message) error {
ev, ok := p.(*hellotestpb.HelloStartedEvent)
if !ok {
return fmt.Errorf("unexpected proto type: %T", p)
}
m.SessionID = ev.SessionId
return nil
}
// joinGreetingServerMsg wraps a JoinGreetingRequest for outbound durable
// delivery. It implements ServerMessage so it can be sent via
// SendClientEventRequest through the DurableActor.
type joinGreetingServerMsg struct {
actor.BaseMessage
// SessionID identifies the greeting session to join.
SessionID string
}
// ServiceMethod returns test routing metadata for the greeting service.
func (m *joinGreetingServerMsg) ServiceMethod() mailboxrpc.ServiceMethod {
return mailboxrpc.ServiceMethod{
Service: "hellotest.v1.HelloService",
Method: "JoinGreeting",
}
}
// ToProto converts to a proto message for mailbox envelope transport.
func (m *joinGreetingServerMsg) ToProto() fn.Result[proto.Message] {
return fn.Ok[proto.Message](&hellotestpb.JoinGreetingRequest{
SessionId: m.SessionID,
})
}
// Compile-time interface checks.
var (
_ InboundServerMessage = (*helloStartedMsg)(nil)
_ ServerMessage = (*joinGreetingServerMsg)(nil)
)
// greetingBehavior is a trivial actor behavior that records received
// helloStartedMsg messages on a channel for test assertions.
type greetingBehavior struct {
received chan *helloStartedMsg
}
// Receive processes a single helloStartedMsg by forwarding it to the
// test's observation channel.
func (b *greetingBehavior) Receive(
ctx context.Context, msg *helloStartedMsg,
) fn.Result[struct{}] {
select {
case b.received <- msg:
case <-ctx.Done():
return fn.Err[struct{}](ctx.Err())
}
return fn.Ok(struct{}{})
}
// testServer simulates the remote server side of a mailbox connection. It
// polls the server-side mailbox for inbound client envelopes and dispatches
// them: KIND_REQUEST envelopes go through a ServeMux, KIND_EVENT envelopes
// are recorded on the received channel.
type testServer struct {
mb *inMemoryMailbox
mux *mailboxrpc.ServeMux
serverMailboxID string
// received tracks fire-and-forget events delivered to the server.
received chan *mailboxpb.Envelope
}
// newTestServer creates a server simulator backed by the given mailbox.
func newTestServer(
mb *inMemoryMailbox, serverMailboxID string,
) *testServer {
return &testServer{
mb: mb,
mux: mailboxrpc.NewServeMux(),
serverMailboxID: serverMailboxID,
received: make(chan *mailboxpb.Envelope, 20),
}
}
// run polls the server mailbox and dispatches incoming envelopes until
// ctx is cancelled.
func (s *testServer) run(ctx context.Context) {
var cursor uint64
for {
select {
case <-ctx.Done():
return
default:
}
envs, next, status := s.mb.pull(
ctx, s.serverMailboxID, cursor, 10, 50*time.Millisecond,
)
if !status.Ok || len(envs) == 0 {
continue
}
cursor = next
for _, env := range envs {
if env.Rpc == nil {
continue
}
switch env.Rpc.Kind {
case mailboxpb.RpcMeta_KIND_REQUEST:
s.handleRequest(ctx, env)
case mailboxpb.RpcMeta_KIND_EVENT:
select {
case s.received <- env:
default:
}
// The test server only cares about REQUEST/EVENT; the
// other KIND_* values are ignored.
case mailboxpb.RpcMeta_KIND_UNSPECIFIED,
mailboxpb.RpcMeta_KIND_RESPONSE:
}
}
}
}
// handleRequest dispatches a KIND_REQUEST envelope through the ServeMux
// and sends the response envelope back to the client's ReplyTo mailbox.
func (s *testServer) handleRequest(ctx context.Context,
env *mailboxpb.Envelope) {
if env.Body == nil {
return
}
respMsg, err := s.mux.ServeRPC(
ctx, env.Rpc.Service, env.Rpc.Method, env.Body.Value,
)
var (
body *anypb.Any
headers map[string]string
)
if err != nil {
// Transport the error via grpc_status headers so the client
// can surface it as a gRPC status error.
headers = mailboxrpc.EncodeErrorHeaders(err)
body = &anypb.Any{}
} else if body, err = anypb.New(respMsg); err != nil {
// If wrapping the response fails (e.g., unregistered type
// URL), surface it as a server-side Internal error so the
// client sees a clear gRPC failure rather than garbled
// bytes.
headers = mailboxrpc.EncodeErrorHeaders(
fmt.Errorf("wrap response in Any: %w", err),
)
body = &anypb.Any{}
}
responseEnv := &mailboxpb.Envelope{
ProtocolVersion: 1,
ArkProtocolVersion: 1,
Sender: s.serverMailboxID,
Recipient: env.Rpc.ReplyTo,
Headers: headers,
Body: body,
Rpc: &mailboxpb.RpcMeta{
Kind: mailboxpb.RpcMeta_KIND_RESPONSE,
CorrelationId: env.Rpc.CorrelationId,
Service: env.Rpc.Service,
Method: env.Rpc.Method,
},
}
s.mb.send(responseEnv)
}
// pushEvent injects a KIND_EVENT envelope into the client's mailbox.
func (s *testServer) pushEvent(t *testing.T, recipientID, service,
method string, event proto.Message) {
t.Helper()
body, err := anypb.New(event)
require.NoError(t, err)
env := &mailboxpb.Envelope{
ProtocolVersion: 1,
ArkProtocolVersion: 1,
Sender: s.serverMailboxID,
Recipient: recipientID,
Body: body,
Rpc: &mailboxpb.RpcMeta{
Kind: mailboxpb.RpcMeta_KIND_EVENT,
Service: service,
Method: method,
ReplyTo: s.serverMailboxID,
},
}
status := s.mb.send(env)
require.True(t, status.Ok, "push event failed: %s", status.Message)
}
// helloServer implements the generated HelloServiceMailboxServer interface.
type helloServer struct{}
// SayHello echoes a greeting containing the caller's name.
func (s *helloServer) SayHello(_ context.Context,
req *hellotestpb.HelloRequest) (*hellotestpb.HelloResponse, error) {
return &hellotestpb.HelloResponse{
Greeting: fmt.Sprintf("Hello, %s!", req.Name),
}, nil
}
// SayGoodbye echoes a farewell containing the caller's name.
func (s *helloServer) SayGoodbye(_ context.Context,
req *hellotestpb.GoodbyeRequest) (*hellotestpb.GoodbyeResponse, error) {
return &hellotestpb.GoodbyeResponse{
Farewell: fmt.Sprintf("Goodbye, %s!", req.Name),
}, nil
}
// TestE2EUnaryRPC verifies a full unary request/response round-trip through
// the mailbox transport. The client sends SayHello via the generated
// HelloServiceMailboxClient, the server simulator dispatches it through
// ServeMux, and the client receives the typed response.
func TestE2EUnaryRPC(t *testing.T) {
t.Parallel()
mb := newInMemoryMailbox()
store := newMemCheckpointStore()
// Server side: register the HelloService handler.
server := newTestServer(mb, "server-1")
hellotestpb.RegisterHelloServiceMailboxServer(
server.mux, &helloServer{},
)
cfg := newTestConnectorConfig(mb, store)
runtime, err := NewRuntime(cfg)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
go server.run(ctx)
require.NoError(t, runtime.Start(ctx))
defer runtime.Stop()
// Issue a unary SayHello RPC via the generated mailbox client.
client := hellotestpb.NewHelloServiceMailboxClient(runtime.Unary())
resp, err := client.SayHello(ctx, &hellotestpb.HelloRequest{
Name: "Alice",
})
require.NoError(t, err)
require.Equal(t, "Hello, Alice!", resp.Greeting)
// Also exercise SayGoodbye to confirm independent method routing.
goodbye, err := client.SayGoodbye(ctx, &hellotestpb.GoodbyeRequest{
Name: "Bob",
})
require.NoError(t, err)
require.Equal(t, "Goodbye, Bob!", goodbye.Farewell)
}
// TestE2EServerPushEvent verifies that a server-pushed KIND_EVENT envelope
// is routed through the EventRouter to a registered greeting actor. The
// test creates a full actor system, registers a greeting actor under a
// ServiceKey, and verifies that the actor receives the deserialized
// helloStartedMsg via the FromProto interface.
func TestE2EServerPushEvent(t *testing.T) {
t.Parallel()
mb := newInMemoryMailbox()
store := newMemCheckpointStore()
system := actor.NewActorSystem()
// Register a greeting actor to receive server push events.
greetingKey := actor.NewServiceKey[*helloStartedMsg, struct{}](
"greeting-actor",
)
behavior := &greetingBehavior{
received: make(chan *helloStartedMsg, 10),
}
actor.RegisterWithSystem(
system, "greeting-1", greetingKey, behavior,
)
// Wire up the EventRouter with the InboundServerMessage-based
// helper. NewEventRoute auto-generates the Adapt function from
// helloStartedMsg.FromProto.
router := NewEventRouter(system)
NewEventRoute(
router, InboundEventRouteConfig[*helloStartedMsg, struct{}]{
Service: "hellotest.v1.HelloService",
Method: "HelloStarted",
Key: greetingKey,
NewEvent: func() proto.Message {
return &hellotestpb.HelloStartedEvent{}
},
NewMsg: func() *helloStartedMsg {
return &helloStartedMsg{}
},
})
server := newTestServer(mb, "server-1")
cfg := newTestConnectorConfig(mb, store)
cfg.Dispatchers = router.AsDispatcherMap()
runtime, err := NewRuntime(cfg)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
go server.run(ctx)
require.NoError(t, runtime.Start(ctx))
defer runtime.Stop()
// Server pushes a HelloStartedEvent to the client.
server.pushEvent(
t, "client-1", "hellotest.v1.HelloService", "HelloStarted",
&hellotestpb.HelloStartedEvent{
SessionId: "session-42",
},
)
// Wait for the greeting actor to receive the dispatched message.
select {
case msg := <-behavior.received:
require.Equal(t, "session-42", msg.SessionID)
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for greeting actor to receive event")
}
}
// TestE2EClientFireAndForget verifies that the client can send a
// fire-and-forget event to the server via the DurableActor egress path.
// The test sends a JoinGreetingRequest through SendClientEventRequest,
// which is durably persisted in the actor's mailbox, serialized to proto
// via ToProto, and sent as a KIND_EVENT envelope to the server.
func TestE2EClientFireAndForget(t *testing.T) {
t.Parallel()
mb := newInMemoryMailbox()
store := newMemCheckpointStore()
server := newTestServer(mb, "server-1")
cfg := newTestConnectorConfig(mb, store)
runtime, err := NewRuntime(cfg)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
go server.run(ctx)
require.NoError(t, runtime.Start(ctx))
defer runtime.Stop()
// Client sends a fire-and-forget event via the DurableActor. The
// message is persisted in the actor mailbox before the DurableActor
// processes it and sends it to the server via Edge.Send.
err = runtime.TellRef().Tell(ctx, &SendClientEventRequest{
Message: &joinGreetingServerMsg{SessionID: "greeting-99"},
})
require.NoError(t, err)
// Wait for the server to receive the event envelope.
select {
case env := <-server.received:
require.NotNil(t, env.Rpc)
require.Equal(t,
mailboxpb.RpcMeta_KIND_EVENT, env.Rpc.Kind,
)
// Unmarshal the body to verify the JoinGreetingRequest
// arrived intact.
var joinReq hellotestpb.JoinGreetingRequest
err := proto.Unmarshal(env.Body.Value, &joinReq)
require.NoError(t, err)
require.Equal(t, "greeting-99", joinReq.SessionId)
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for server to receive event")
}
}
// TestE2EUnaryAndPush verifies a combined scenario where the client
// issues a unary RPC and also receives server-push events in the same
// session. This exercises concurrent ingress (response delivery +
// event dispatch) with the EventRouter and UnaryFacade both active.
func TestE2EUnaryAndPush(t *testing.T) {
t.Parallel()
mb := newInMemoryMailbox()
store := newMemCheckpointStore()
system := actor.NewActorSystem()
// Register greeting actor.
greetingKey := actor.NewServiceKey[*helloStartedMsg, struct{}](
"greeting-actor",
)
behavior := &greetingBehavior{
received: make(chan *helloStartedMsg, 10),
}
actor.RegisterWithSystem(
system, "greeting-1", greetingKey, behavior,
)
// Wire EventRouter.
router := NewEventRouter(system)
NewEventRoute(
router, InboundEventRouteConfig[*helloStartedMsg, struct{}]{
Service: "hellotest.v1.HelloService",
Method: "HelloStarted",
Key: greetingKey,
NewEvent: func() proto.Message {
return &hellotestpb.HelloStartedEvent{}
},
NewMsg: func() *helloStartedMsg {
return &helloStartedMsg{}
},
})
// Server with HelloService handler.
server := newTestServer(mb, "server-1")
hellotestpb.RegisterHelloServiceMailboxServer(
server.mux, &helloServer{},
)
cfg := newTestConnectorConfig(mb, store)
cfg.Dispatchers = router.AsDispatcherMap()
runtime, err := NewRuntime(cfg)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
go server.run(ctx)
require.NoError(t, runtime.Start(ctx))
defer runtime.Stop()
// Phase 1: Server pushes an event before the client sends an RPC.
server.pushEvent(
t, "client-1", "hellotest.v1.HelloService", "HelloStarted",
&hellotestpb.HelloStartedEvent{
SessionId: "pre-rpc",
},
)
select {
case msg := <-behavior.received:
require.Equal(t, "pre-rpc", msg.SessionID)
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for pre-RPC push event")
}
// Phase 2: Client issues a unary SayHello RPC.
client := hellotestpb.NewHelloServiceMailboxClient(runtime.Unary())
resp, err := client.SayHello(ctx, &hellotestpb.HelloRequest{
Name: "Charlie",
})
require.NoError(t, err)
require.Equal(t, "Hello, Charlie!", resp.Greeting)
// Phase 3: Server pushes another event after the RPC.
server.pushEvent(
t, "client-1", "hellotest.v1.HelloService", "HelloStarted",
&hellotestpb.HelloStartedEvent{
SessionId: "post-rpc",
},
)
select {
case msg := <-behavior.received:
require.Equal(t, "post-rpc", msg.SessionID)
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for post-RPC push event")
}
}
// errHelloServer implements HelloServiceMailboxServer with handlers that
// return gRPC status errors so the error-header transport path can be
// exercised end-to-end.
type errHelloServer struct{}
// SayHello returns a gRPC NotFound error for any request.
func (s *errHelloServer) SayHello(_ context.Context,
_ *hellotestpb.HelloRequest) (*hellotestpb.HelloResponse, error) {
return nil, status.Errorf(codes.NotFound, "user not found")
}
// SayGoodbye returns a gRPC InvalidArgument error for any request.
func (s *errHelloServer) SayGoodbye(_ context.Context,
_ *hellotestpb.GoodbyeRequest) (*hellotestpb.GoodbyeResponse, error) {
return nil, status.Errorf(codes.InvalidArgument, "name is required")
}
// TestE2EUnaryRPCError verifies that a server-side gRPC error is
// transported through the mailbox envelope headers and surfaced to the
// client as a proper gRPC status error via AwaitRPC. This exercises the
// EncodeErrorHeaders → HeaderGRPCStatusB64 → DecodeErrorHeaders path
// end-to-end.
func TestE2EUnaryRPCError(t *testing.T) {
t.Parallel()
mb := newInMemoryMailbox()
store := newMemCheckpointStore()
// Server side: register a handler that always returns errors.
server := newTestServer(mb, "server-1")
hellotestpb.RegisterHelloServiceMailboxServer(
server.mux, &errHelloServer{},
)
cfg := newTestConnectorConfig(mb, store)
runtime, err := NewRuntime(cfg)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
go server.run(ctx)
require.NoError(t, runtime.Start(ctx))
defer runtime.Stop()
client := hellotestpb.NewHelloServiceMailboxClient(runtime.Unary())
// SayHello should surface a NotFound gRPC error.
_, helloErr := client.SayHello(ctx, &hellotestpb.HelloRequest{
Name: "Alice",
})
require.Error(t, helloErr)
st, ok := status.FromError(helloErr)
require.True(t, ok, "expected gRPC status error, got: %v", helloErr)
require.Equal(t, codes.NotFound, st.Code())
require.Contains(t, st.Message(), "user not found")
// SayGoodbye should surface an InvalidArgument gRPC error.
_, goodbyeErr := client.SayGoodbye(
ctx, &hellotestpb.GoodbyeRequest{
Name: "Bob",
},
)
require.Error(t, goodbyeErr)
st, ok = status.FromError(goodbyeErr)
require.True(t, ok, "expected gRPC status error, got: %v", goodbyeErr)
require.Equal(t, codes.InvalidArgument, st.Code())
require.Contains(t, st.Message(), "name is required")
}