package store import ( "context" "fmt" "sync" "testing" "github.com/misaki/Schedule/server/internal/model" ) func TestConcurrentAppendPreservesAllChanges(t *testing.T) { st := newStore(t) ctx := context.Background() const devices = 2 const batchesPerDevice = 50 const changesPerBatch = 10 const total = devices * batchesPerDevice * changesPerBatch var wg sync.WaitGroup errs := make(chan error, devices) for d := 0; d < devices; d++ { wg.Add(1) go func(device int) { defer wg.Done() for b := 0; b < batchesPerDevice; b++ { changes := make([]model.ChangeInput, 0, changesPerBatch) for i := 0; i < changesPerBatch; i++ { id := fmt.Sprintf("d%d-b%d-%d", device, b, i) changes = append(changes, model.ChangeInput{ EntityType: "task", EntityID: id, Op: "upsert", Version: 1, UpdatedAt: "2026-10-26T10:00:00Z", Payload: []byte(`{"id":"` + id + `"}`), }) } if _, err := st.AppendChanges(ctx, fmt.Sprintf("device-%d", device), changes); err != nil { errs <- err return } } }(d) } wg.Wait() close(errs) for err := range errs { t.Fatalf("concurrent append: %v", err) } all, latest, hasMore, err := st.ChangesSince(ctx, 0, total+10) if err != nil { t.Fatalf("pull all: %v", err) } if len(all) != total || latest != total || hasMore { t.Fatalf("changes=%d latest=%d hasMore=%v", len(all), latest, hasMore) } seen := make(map[int64]struct{}, len(all)) for _, change := range all { if _, ok := seen[change.Seq]; ok { t.Fatalf("duplicate seq %d", change.Seq) } seen[change.Seq] = struct{}{} } }