(t *testing.T)
| 16 | ) |
| 17 | |
| 18 | func TestServiceMap(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | ctx, cancel := context.WithCancel(context.Background()) |
| 21 | t.Cleanup(cancel) |
| 22 | sup := suture.NewSimple("TestServiceMap") |
| 23 | sup.ServeBackground(ctx) |
| 24 | |
| 25 | t.Run("SimpleAddRemove", func(t *testing.T) { |
| 26 | t.Parallel() |
| 27 | |
| 28 | sm := newServiceMap[string, *dummyService](events.NoopLogger) |
| 29 | sup.Add(sm) |
| 30 | |
| 31 | // Add two services. They should start. |
| 32 | |
| 33 | d1 := newDummyService() |
| 34 | d2 := newDummyService() |
| 35 | |
| 36 | sm.Add("d1", d1) |
| 37 | sm.Add("d2", d2) |
| 38 | |
| 39 | <-d1.started |
| 40 | <-d2.started |
| 41 | |
| 42 | // Remove them. They should stop. |
| 43 | |
| 44 | if !sm.Remove("d1") { |
| 45 | t.Errorf("Remove failed") |
| 46 | } |
| 47 | if !sm.Remove("d2") { |
| 48 | t.Errorf("Remove failed") |
| 49 | } |
| 50 | |
| 51 | <-d1.stopped |
| 52 | <-d2.stopped |
| 53 | }) |
| 54 | |
| 55 | t.Run("OverwriteImpliesRemove", func(t *testing.T) { |
| 56 | t.Parallel() |
| 57 | |
| 58 | sm := newServiceMap[string, *dummyService](events.NoopLogger) |
| 59 | sup.Add(sm) |
| 60 | |
| 61 | d1 := newDummyService() |
| 62 | d2 := newDummyService() |
| 63 | |
| 64 | // Add d1, it should start. |
| 65 | |
| 66 | sm.Add("k", d1) |
| 67 | <-d1.started |
| 68 | |
| 69 | // Add d2, with the same key. The previous one should stop as we're |
| 70 | // doing a replace. |
| 71 | |
| 72 | sm.Add("k", d2) |
| 73 | <-d1.stopped |
| 74 | <-d2.started |
| 75 |
nothing calls this directly
no test coverage detected