|
| 1 | +package lifecycle_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/exanubes/appsync/internal/app" |
| 10 | + "github.com/exanubes/appsync/internal/app/lifecycle" |
| 11 | +) |
| 12 | + |
| 13 | +var errCustom = errors.New("custom error") |
| 14 | + |
| 15 | +func closed_state(cause error) *lifecycle.State { |
| 16 | + s := lifecycle.NewState() |
| 17 | + s.Close(cause) |
| 18 | + return s |
| 19 | +} |
| 20 | + |
| 21 | +func TestNewState(t *testing.T) { |
| 22 | + t.Run("done channel is open", func(t *testing.T) { |
| 23 | + s := lifecycle.NewState() |
| 24 | + select { |
| 25 | + case <-s.Done(): |
| 26 | + t.Fatal("Done() should not be readable on a new state") |
| 27 | + default: |
| 28 | + } |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +func TestDone(t *testing.T) { |
| 33 | + t.Run("blocks before Close", func(t *testing.T) { |
| 34 | + s := lifecycle.NewState() |
| 35 | + select { |
| 36 | + case <-s.Done(): |
| 37 | + t.Fatal("Done() should block before Close()") |
| 38 | + default: |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("readable after Close", func(t *testing.T) { |
| 43 | + s := closed_state(nil) |
| 44 | + select { |
| 45 | + case <-s.Done(): |
| 46 | + case <-time.After(100 * time.Millisecond): |
| 47 | + t.Fatal("Done() should be readable after Close()") |
| 48 | + } |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func TestErr(t *testing.T) { |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + state func() *lifecycle.State |
| 56 | + is_closed bool |
| 57 | + is_custom bool |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "returns nil before Close", |
| 61 | + state: lifecycle.NewState, |
| 62 | + is_closed: false, |
| 63 | + is_custom: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "returns ErrConnectionClosed when closed with nil", |
| 67 | + state: func() *lifecycle.State { return closed_state(nil) }, |
| 68 | + is_closed: true, |
| 69 | + is_custom: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "returns ErrConnectionClosed when closed with ErrConnectionClosed", |
| 73 | + state: func() *lifecycle.State { return closed_state(app.ErrConnectionClosed) }, |
| 74 | + is_closed: true, |
| 75 | + is_custom: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "wraps custom error with ErrConnectionClosed", |
| 79 | + state: func() *lifecycle.State { return closed_state(errCustom) }, |
| 80 | + is_closed: true, |
| 81 | + is_custom: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "preserves pre-joined error containing ErrConnectionClosed and custom error", |
| 85 | + state: func() *lifecycle.State { return closed_state(errors.Join(app.ErrConnectionClosed, errCustom)) }, |
| 86 | + is_closed: true, |
| 87 | + is_custom: true, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + for _, tt := range tests { |
| 92 | + t.Run(tt.name, func(t *testing.T) { |
| 93 | + err := tt.state().Err() |
| 94 | + |
| 95 | + if !tt.is_closed { |
| 96 | + if err != nil { |
| 97 | + t.Errorf("Err() = %v, want nil", err) |
| 98 | + } |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + if err == nil { |
| 103 | + t.Fatal("Err() = nil, want non-nil error") |
| 104 | + } |
| 105 | + if !errors.Is(err, app.ErrConnectionClosed) { |
| 106 | + t.Errorf("Err() = %v, want errors.Is(err, ErrConnectionClosed) == true", err) |
| 107 | + } |
| 108 | + if tt.is_custom && !errors.Is(err, errCustom) { |
| 109 | + t.Errorf("Err() = %v, want errors.Is(err, errCustom) == true", err) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestClose(t *testing.T) { |
| 116 | + t.Run("closes Done channel", func(t *testing.T) { |
| 117 | + s := lifecycle.NewState() |
| 118 | + s.Close(nil) |
| 119 | + select { |
| 120 | + case <-s.Done(): |
| 121 | + case <-time.After(100 * time.Millisecond): |
| 122 | + t.Fatal("Done() should be closed after Close()") |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("idempotent: second call does not overwrite first", func(t *testing.T) { |
| 127 | + s := lifecycle.NewState() |
| 128 | + s.Close(errCustom) |
| 129 | + s.Close(nil) |
| 130 | + |
| 131 | + if err := s.Err(); !errors.Is(err, errCustom) { |
| 132 | + t.Errorf("Err() = %v, want errors.Is(err, errCustom) == true after second Close(nil)", err) |
| 133 | + } |
| 134 | + }) |
| 135 | + |
| 136 | + t.Run("concurrent calls are safe and close exactly once", func(t *testing.T) { |
| 137 | + const goroutines = 100 |
| 138 | + s := lifecycle.NewState() |
| 139 | + |
| 140 | + var wg sync.WaitGroup |
| 141 | + wg.Add(goroutines) |
| 142 | + start := make(chan struct{}) |
| 143 | + |
| 144 | + for range goroutines { |
| 145 | + go func() { |
| 146 | + defer wg.Done() |
| 147 | + <-start |
| 148 | + s.Close(errCustom) |
| 149 | + }() |
| 150 | + } |
| 151 | + |
| 152 | + close(start) |
| 153 | + wg.Wait() |
| 154 | + |
| 155 | + if err := s.Err(); err == nil { |
| 156 | + t.Fatal("Err() = nil after concurrent Close calls, want non-nil") |
| 157 | + } |
| 158 | + }) |
| 159 | +} |
0 commit comments