-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathnode_service_test.go
More file actions
119 lines (102 loc) · 3.49 KB
/
Copy pathnode_service_test.go
File metadata and controls
119 lines (102 loc) · 3.49 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
package node_test
import (
"context"
"errors"
"testing"
"time"
"github.com/NethermindEth/juno/blockchain/networks"
"github.com/NethermindEth/juno/node"
"github.com/NethermindEth/juno/utils/log"
"github.com/sourcegraph/conc"
"github.com/stretchr/testify/require"
)
// fakeService lets each test drive what Run does.
type fakeService struct {
run func(ctx context.Context) error
}
func (f *fakeService) Run(ctx context.Context) error { return f.run(ctx) }
// TestStartService covers the three ways a service's Run can return and the
// expected effect on the node-wide context:
// - error -> shut the node down
// - panic -> shut the node down and propagate the panic
// - nil -> let the service exit without taking the node down
func TestStartService(t *testing.T) {
// newNode builds a node through the public constructor with the minimal
// offline config also used by TestNetworkVerificationOnNonEmptyDB.
newNode := func(t *testing.T) *node.Node {
n, err := node.New(&node.Config{
DatabasePath: t.TempDir(),
DBCompression: "zstd",
Network: networks.Sepolia,
DisableL1Verification: true,
SubmittedTransactionsCacheEntryTTL: time.Second,
}, "test", log.NewLevel(log.INFO))
require.NoError(t, err)
return n
}
t.Run("error shuts the node down", func(t *testing.T) {
n := newNode(t)
wg := conc.NewWaitGroup()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
failing := &fakeService{run: func(context.Context) error {
return errors.New("boom")
}}
n.StartService(wg, ctx, cancel, failing)
wg.Wait()
require.Error(t, ctx.Err(), "a service error should shut the node down")
})
t.Run("panic shuts the node down and propagates", func(t *testing.T) {
n := newNode(t)
wg := conc.NewWaitGroup()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
panicking := &fakeService{run: func(context.Context) error {
panic("boom")
}}
n.StartService(wg, ctx, cancel, panicking)
require.Panics(t, func() { wg.Wait() }, "a panicking service should propagate the panic")
require.Error(t, ctx.Err(), "a panicking service should shut the node down")
})
t.Run("clean return keeps the node running", func(t *testing.T) {
n := newNode(t)
wg := conc.NewWaitGroup()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
// Short-lived service: finishes its work and returns nil.
exited := make(chan struct{})
shortLived := &fakeService{run: func(context.Context) error {
close(exited)
return nil
}}
// Long-running service: lives until the node is shut down.
stopped := make(chan struct{})
longRunning := &fakeService{run: func(ctx context.Context) error {
<-ctx.Done()
close(stopped)
return nil
}}
n.StartService(wg, ctx, cancel, longRunning)
n.StartService(wg, ctx, cancel, shortLived)
select {
case <-exited: // the short-lived service has returned cleanly
case <-time.After(10 * time.Second):
t.Fatal("short-lived service did not exit in time")
}
// Its clean exit must not take the long-running service down with it.
select {
case <-stopped:
t.Fatal("long-running service stopped: node was shut down unexpectedly")
default:
}
require.NoError(t, ctx.Err(), "a clean exit should not shut the node down")
// A real shutdown still stops everything.
cancel()
wg.Wait()
select {
case <-stopped:
case <-time.After(10 * time.Second):
t.Fatal("long-running service did not stop in time")
}
})
}