Tests that services are restarted cleanly as new instances.
(t *testing.T)
| 173 | |
| 174 | // Tests that services are restarted cleanly as new instances. |
| 175 | func TestServiceRestarts(t *testing.T) { |
| 176 | stack, err := New(testNodeConfig()) |
| 177 | if err != nil { |
| 178 | t.Fatalf("failed to create protocol stack: %v", err) |
| 179 | } |
| 180 | // Define a service that does not support restarts |
| 181 | var ( |
| 182 | running bool |
| 183 | started int |
| 184 | ) |
| 185 | constructor := func(*ServiceContext) (Service, error) { |
| 186 | running = false |
| 187 | |
| 188 | return &InstrumentedService{ |
| 189 | startHook: func(*p2p.Server) { |
| 190 | if running { |
| 191 | panic("already running") |
| 192 | } |
| 193 | running = true |
| 194 | started++ |
| 195 | }, |
| 196 | }, nil |
| 197 | } |
| 198 | // Register the service and start the protocol stack |
| 199 | if err := stack.Register(constructor); err != nil { |
| 200 | t.Fatalf("failed to register the service: %v", err) |
| 201 | } |
| 202 | if err := stack.Start(); err != nil { |
| 203 | t.Fatalf("failed to start protocol stack: %v", err) |
| 204 | } |
| 205 | defer stack.Stop() |
| 206 | |
| 207 | if !running || started != 1 { |
| 208 | t.Fatalf("running/started mismatch: have %v/%d, want true/1", running, started) |
| 209 | } |
| 210 | // Restart the stack a few times and check successful service restarts |
| 211 | for i := 0; i < 3; i++ { |
| 212 | if err := stack.Restart(); err != nil { |
| 213 | t.Fatalf("iter %d: failed to restart stack: %v", i, err) |
| 214 | } |
| 215 | } |
| 216 | if !running || started != 4 { |
| 217 | t.Fatalf("running/started mismatch: have %v/%d, want true/4", running, started) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // Tests that if a service fails to initialize itself, none of the other services |
| 222 | // will be allowed to even start. |