Skip to content

Commit 8be0890

Browse files
GeorgeTsagkguggero
authored andcommitted
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 9c98b18 commit 8be0890

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

itest/assets_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,112 @@ func assertMinNumHtlcs(t *testing.T, node *HarnessNode, expected int) {
23202320
require.NoError(t, err)
23212321
}
23222322

2323+
type subscribeEventsClient = routerrpc.Router_SubscribeHtlcEventsClient
2324+
2325+
type htlcEventConfig struct {
2326+
timeout time.Duration
2327+
numEvents int
2328+
withLinkFailure bool
2329+
withFailureDetail routerrpc.FailureDetail
2330+
}
2331+
2332+
func defaultHtlcEventConfig() *htlcEventConfig {
2333+
return &htlcEventConfig{
2334+
timeout: defaultTimeout,
2335+
}
2336+
}
2337+
2338+
type htlcEventOpt func(*htlcEventConfig)
2339+
2340+
func withTimeout(timeout time.Duration) htlcEventOpt {
2341+
return func(config *htlcEventConfig) {
2342+
config.timeout = timeout
2343+
}
2344+
}
2345+
2346+
func withNumEvents(numEvents int) htlcEventOpt {
2347+
return func(config *htlcEventConfig) {
2348+
config.numEvents = numEvents
2349+
}
2350+
}
2351+
2352+
func withLinkFailure(detail routerrpc.FailureDetail) htlcEventOpt {
2353+
return func(config *htlcEventConfig) {
2354+
config.withLinkFailure = true
2355+
config.withFailureDetail = detail
2356+
}
2357+
}
2358+
2359+
func assertHtlcEvents(t *testing.T, c subscribeEventsClient,
2360+
opts ...htlcEventOpt) {
2361+
2362+
t.Helper()
2363+
2364+
cfg := defaultHtlcEventConfig()
2365+
for _, opt := range opts {
2366+
opt(cfg)
2367+
}
2368+
2369+
timeout := time.After(cfg.timeout)
2370+
events := make(chan *routerrpc.HtlcEvent)
2371+
2372+
go func() {
2373+
defer close(events)
2374+
2375+
for {
2376+
evt, err := c.Recv()
2377+
if err != nil {
2378+
t.Logf("Received HTLC event error: %v", err)
2379+
return
2380+
}
2381+
2382+
select {
2383+
case events <- evt:
2384+
case <-timeout:
2385+
t.Logf("Htlc event receive timeout")
2386+
return
2387+
}
2388+
}
2389+
}()
2390+
2391+
var numEvents int
2392+
for {
2393+
type linkFailEvent = *routerrpc.HtlcEvent_LinkFailEvent
2394+
2395+
select {
2396+
case evt, ok := <-events:
2397+
if !ok {
2398+
t.Fatalf("Htlc event stream closed")
2399+
return
2400+
}
2401+
2402+
if cfg.withLinkFailure {
2403+
linkEvent, ok := evt.Event.(linkFailEvent)
2404+
if !ok {
2405+
// We only count link failure events.
2406+
continue
2407+
}
2408+
2409+
if linkEvent.LinkFailEvent.FailureDetail !=
2410+
cfg.withFailureDetail {
2411+
2412+
continue
2413+
}
2414+
}
2415+
2416+
numEvents++
2417+
2418+
if numEvents == cfg.numEvents {
2419+
return
2420+
}
2421+
2422+
case <-timeout:
2423+
t.Fatalf("Htlc event receive timeout")
2424+
return
2425+
}
2426+
}
2427+
}
2428+
23232429
func assertNumHtlcs(t *testing.T, node *HarnessNode, expected int) {
23242430
t.Helper()
23252431

0 commit comments

Comments
 (0)