Files
Schedule/server/internal/store/sqlite_concurrency_test.go
Misaki 5be7f7f04b feat: implement AI schedule app with multi-device sync
- Qt 6 desktop client with day/month/year views, AI scheduling, and TOML settings\n- Go sync server with SQLite change stream, WebSocket notifications, and Docker deployment\n- Client outbox/LWW sync, offline retry, and dual-client integration tests\n- Linux fcitx5 setup, validation reports, and Windows MinGW preset
2026-10-26 13:58:45 +08:00

68 lines
1.6 KiB
Go

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{}{}
}
}