(t *testing.T)
| 149 | } |
| 150 | |
| 151 | func TestRepositoryConcurrentAppend(t *testing.T) { |
| 152 | t.Parallel() |
| 153 | |
| 154 | repository := testRepository(t, nil) |
| 155 | |
| 156 | state, err := repository.Create(State{ |
| 157 | Service: ServiceDocs, |
| 158 | DocumentID: "doc1", |
| 159 | Account: "user@example.com", |
| 160 | Client: "default", |
| 161 | }) |
| 162 | if err != nil { |
| 163 | t.Fatalf("Create: %v", err) |
| 164 | } |
| 165 | |
| 166 | const appendCount = 20 |
| 167 | var wg sync.WaitGroup |
| 168 | |
| 169 | errs := make(chan error, appendCount) |
| 170 | for index := range appendCount { |
| 171 | wg.Add(1) |
| 172 | |
| 173 | go func() { |
| 174 | defer wg.Done() |
| 175 | |
| 176 | request, marshalErr := json.Marshal(map[string]int{"index": index}) |
| 177 | if marshalErr != nil { |
| 178 | errs <- marshalErr |
| 179 | |
| 180 | return |
| 181 | } |
| 182 | |
| 183 | _, appendErr := repository.Append(AppendOptions{ |
| 184 | BatchID: state.BatchID, |
| 185 | Command: "docs.insert", |
| 186 | Identity: testIdentity("doc1"), |
| 187 | RevisionID: "rev1", |
| 188 | Requests: []json.RawMessage{request}, |
| 189 | }) |
| 190 | errs <- appendErr |
| 191 | }() |
| 192 | } |
| 193 | |
| 194 | wg.Wait() |
| 195 | close(errs) |
| 196 | |
| 197 | for err := range errs { |
| 198 | if err != nil { |
| 199 | t.Fatalf("Append: %v", err) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | loaded, err := repository.Get(state.BatchID) |
| 204 | if err != nil { |
| 205 | t.Fatalf("Get: %v", err) |
| 206 | } |
| 207 | |
| 208 | if len(loaded.Requests) != appendCount { |
nothing calls this directly
no test coverage detected