MCPcopy Create free account
hub / github.com/CPChain/chain / TestServiceLifeCycle

Function TestServiceLifeCycle

node/node_test.go:125–172  ·  view source on GitHub ↗

Tests that registered services get started and stopped correctly.

(t *testing.T)

Source from the content-addressed store, hash-verified

123
124// Tests that registered services get started and stopped correctly.
125func TestServiceLifeCycle(t *testing.T) {
126 stack, err := New(testNodeConfig())
127 if err != nil {
128 t.Fatalf("failed to create protocol stack: %v", err)
129 }
130 // Register a batch of life-cycle instrumented services
131 services := map[string]InstrumentingWrapper{
132 "A": InstrumentedServiceMakerA,
133 "B": InstrumentedServiceMakerB,
134 "C": InstrumentedServiceMakerC,
135 }
136 started := make(map[string]bool)
137 stopped := make(map[string]bool)
138
139 for id, maker := range services {
140 id := id // Closure for the constructor
141 constructor := func(*ServiceContext) (Service, error) {
142 return &InstrumentedService{
143 startHook: func(*p2p.Server) { started[id] = true },
144 stopHook: func() { stopped[id] = true },
145 }, nil
146 }
147 if err := stack.Register(maker(constructor)); err != nil {
148 t.Fatalf("service %s: registration failed: %v", id, err)
149 }
150 }
151 // Start the node and check that all services are running
152 if err := stack.Start(); err != nil {
153 t.Fatalf("failed to start protocol stack: %v", err)
154 }
155 for id := range services {
156 if !started[id] {
157 t.Fatalf("service %s: freshly started service not running", id)
158 }
159 if stopped[id] {
160 t.Fatalf("service %s: freshly started service already stopped", id)
161 }
162 }
163 // Stop the node and check that all services have been stopped
164 if err := stack.Stop(); err != nil {
165 t.Fatalf("failed to stop protocol stack: %v", err)
166 }
167 for id := range services {
168 if !stopped[id] {
169 t.Fatalf("service %s: freshly terminated service still running", id)
170 }
171 }
172}
173
174// Tests that services are restarted cleanly as new instances.
175func TestServiceRestarts(t *testing.T) {

Callers

nothing calls this directly

Calls 5

testNodeConfigFunction · 0.85
NewFunction · 0.70
StartMethod · 0.65
StopMethod · 0.65
RegisterMethod · 0.45

Tested by

no test coverage detected