| 171 | } |
| 172 | |
| 173 | func TestJSONArrayDynamic(t *testing.T) { |
| 174 | noctx := context.Background() |
| 175 | type mock struct { |
| 176 | ID int `json:"id"` |
| 177 | } |
| 178 | type args[T comparable] struct { |
| 179 | ctx context.Context |
| 180 | f func(ch chan<- *mock, cherr chan<- error) |
| 181 | } |
| 182 | type testCase[T comparable] struct { |
| 183 | name string |
| 184 | args args[T] |
| 185 | len int |
| 186 | wantErr bool |
| 187 | } |
| 188 | |
| 189 | tests := []testCase[*mock]{ |
| 190 | { |
| 191 | name: "happy path", |
| 192 | args: args[*mock]{ |
| 193 | ctx: noctx, |
| 194 | f: func(ch chan<- *mock, _ chan<- error) { |
| 195 | defer close(ch) |
| 196 | ch <- &mock{ID: 1} |
| 197 | }, |
| 198 | }, |
| 199 | len: 1, |
| 200 | wantErr: false, |
| 201 | }, |
| 202 | { |
| 203 | name: "empty array response", |
| 204 | args: args[*mock]{ |
| 205 | ctx: noctx, |
| 206 | f: func(ch chan<- *mock, _ chan<- error) { |
| 207 | close(ch) |
| 208 | }, |
| 209 | }, |
| 210 | len: 0, |
| 211 | wantErr: false, |
| 212 | }, |
| 213 | { |
| 214 | name: "error at beginning of the stream", |
| 215 | args: args[*mock]{ |
| 216 | ctx: noctx, |
| 217 | f: func(ch chan<- *mock, cherr chan<- error) { |
| 218 | defer close(ch) |
| 219 | defer close(cherr) |
| 220 | cherr <- errors.New("error writing to the response writer") |
| 221 | }, |
| 222 | }, |
| 223 | len: 0, |
| 224 | wantErr: true, |
| 225 | }, |
| 226 | { |
| 227 | name: "error while streaming", |
| 228 | args: args[*mock]{ |
| 229 | ctx: noctx, |
| 230 | f: func(ch chan<- *mock, cherr chan<- error) { |