@@ -2519,6 +2519,112 @@ func assertMinNumHtlcs(t *testing.T, node *HarnessNode, expected int) {
25192519 require .NoError (t , err )
25202520}
25212521
2522+ type subscribeEventsClient = routerrpc.Router_SubscribeHtlcEventsClient
2523+
2524+ type htlcEventConfig struct {
2525+ timeout time.Duration
2526+ numEvents int
2527+ withLinkFailure bool
2528+ withFailureDetail routerrpc.FailureDetail
2529+ }
2530+
2531+ func defaultHtlcEventConfig () * htlcEventConfig {
2532+ return & htlcEventConfig {
2533+ timeout : defaultTimeout ,
2534+ }
2535+ }
2536+
2537+ type htlcEventOpt func (* htlcEventConfig )
2538+
2539+ func withTimeout (timeout time.Duration ) htlcEventOpt {
2540+ return func (config * htlcEventConfig ) {
2541+ config .timeout = timeout
2542+ }
2543+ }
2544+
2545+ func withNumEvents (numEvents int ) htlcEventOpt {
2546+ return func (config * htlcEventConfig ) {
2547+ config .numEvents = numEvents
2548+ }
2549+ }
2550+
2551+ func withLinkFailure (detail routerrpc.FailureDetail ) htlcEventOpt {
2552+ return func (config * htlcEventConfig ) {
2553+ config .withLinkFailure = true
2554+ config .withFailureDetail = detail
2555+ }
2556+ }
2557+
2558+ func assertHtlcEvents (t * testing.T , c subscribeEventsClient ,
2559+ opts ... htlcEventOpt ) {
2560+
2561+ t .Helper ()
2562+
2563+ cfg := defaultHtlcEventConfig ()
2564+ for _ , opt := range opts {
2565+ opt (cfg )
2566+ }
2567+
2568+ timeout := time .After (cfg .timeout )
2569+ events := make (chan * routerrpc.HtlcEvent )
2570+
2571+ go func () {
2572+ defer close (events )
2573+
2574+ for {
2575+ evt , err := c .Recv ()
2576+ if err != nil {
2577+ t .Logf ("Received HTLC event error: %v" , err )
2578+ return
2579+ }
2580+
2581+ select {
2582+ case events <- evt :
2583+ case <- timeout :
2584+ t .Logf ("Htlc event receive timeout" )
2585+ return
2586+ }
2587+ }
2588+ }()
2589+
2590+ var numEvents int
2591+ for {
2592+ type linkFailEvent = * routerrpc.HtlcEvent_LinkFailEvent
2593+
2594+ select {
2595+ case evt , ok := <- events :
2596+ if ! ok {
2597+ t .Fatalf ("Htlc event stream closed" )
2598+ return
2599+ }
2600+
2601+ if cfg .withLinkFailure {
2602+ linkEvent , ok := evt .Event .(linkFailEvent )
2603+ if ! ok {
2604+ // We only count link failure events.
2605+ continue
2606+ }
2607+
2608+ if linkEvent .LinkFailEvent .FailureDetail !=
2609+ cfg .withFailureDetail {
2610+
2611+ continue
2612+ }
2613+ }
2614+
2615+ numEvents ++
2616+
2617+ if numEvents == cfg .numEvents {
2618+ return
2619+ }
2620+
2621+ case <- timeout :
2622+ t .Fatalf ("Htlc event receive timeout" )
2623+ return
2624+ }
2625+ }
2626+ }
2627+
25222628func assertNumHtlcs (t * testing.T , node * HarnessNode , expected int ) {
25232629 t .Helper ()
25242630
0 commit comments