Skip to content

Commit 1a7c249

Browse files
committed
itest: add assertHtlcEvents helper
To further extend the check of whether an HTLC was added over a channel we add a helper that checks for HTLC events. This will also be used in the follow-up commit.
1 parent c454672 commit 1a7c249

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

itest/assets_test.go

+106
Original file line numberDiff line numberDiff line change
@@ -2575,6 +2575,112 @@ func assertMinNumHtlcs(t *testing.T, node *HarnessNode, expected int) {
25752575
require.NoError(t, err)
25762576
}
25772577

2578+
type subscribeEventsClient = routerrpc.Router_SubscribeHtlcEventsClient
2579+
2580+
type htlcEventConfig struct {
2581+
timeout time.Duration
2582+
numEvents int
2583+
withLinkFailure bool
2584+
withFailureDetail routerrpc.FailureDetail
2585+
}
2586+
2587+
func defaultHtlcEventConfig() *htlcEventConfig {
2588+
return &htlcEventConfig{
2589+
timeout: defaultTimeout,
2590+
}
2591+
}
2592+
2593+
type htlcEventOpt func(*htlcEventConfig)
2594+
2595+
func withTimeout(timeout time.Duration) htlcEventOpt {
2596+
return func(config *htlcEventConfig) {
2597+
config.timeout = timeout
2598+
}
2599+
}
2600+
2601+
func withNumEvents(numEvents int) htlcEventOpt {
2602+
return func(config *htlcEventConfig) {
2603+
config.numEvents = numEvents
2604+
}
2605+
}
2606+
2607+
func withLinkFailure(detail routerrpc.FailureDetail) htlcEventOpt {
2608+
return func(config *htlcEventConfig) {
2609+
config.withLinkFailure = true
2610+
config.withFailureDetail = detail
2611+
}
2612+
}
2613+
2614+
func assertHtlcEvents(t *testing.T, c subscribeEventsClient,
2615+
opts ...htlcEventOpt) {
2616+
2617+
t.Helper()
2618+
2619+
cfg := defaultHtlcEventConfig()
2620+
for _, opt := range opts {
2621+
opt(cfg)
2622+
}
2623+
2624+
timeout := time.After(cfg.timeout)
2625+
events := make(chan *routerrpc.HtlcEvent)
2626+
2627+
go func() {
2628+
defer close(events)
2629+
2630+
for {
2631+
evt, err := c.Recv()
2632+
if err != nil {
2633+
t.Logf("Received HTLC event error: %v", err)
2634+
return
2635+
}
2636+
2637+
select {
2638+
case events <- evt:
2639+
case <-timeout:
2640+
t.Logf("Htlc event receive timeout")
2641+
return
2642+
}
2643+
}
2644+
}()
2645+
2646+
var numEvents int
2647+
for {
2648+
type linkFailEvent = *routerrpc.HtlcEvent_LinkFailEvent
2649+
2650+
select {
2651+
case evt, ok := <-events:
2652+
if !ok {
2653+
t.Fatalf("Htlc event stream closed")
2654+
return
2655+
}
2656+
2657+
if cfg.withLinkFailure {
2658+
linkEvent, ok := evt.Event.(linkFailEvent)
2659+
if !ok {
2660+
// We only count link failure events.
2661+
continue
2662+
}
2663+
2664+
if linkEvent.LinkFailEvent.FailureDetail !=
2665+
cfg.withFailureDetail {
2666+
2667+
continue
2668+
}
2669+
}
2670+
2671+
numEvents++
2672+
2673+
if numEvents == cfg.numEvents {
2674+
return
2675+
}
2676+
2677+
case <-timeout:
2678+
t.Fatalf("Htlc event receive timeout")
2679+
return
2680+
}
2681+
}
2682+
}
2683+
25782684
func assertNumHtlcs(t *testing.T, node *HarnessNode, expected int) {
25792685
t.Helper()
25802686

0 commit comments

Comments
 (0)