返回 DeepSeek-Reasonix
session_catalog_metadata_worker_test.go
根目录 / desktop / session_catalog_metadata_worker_test.go
1 package main
2
3 import (
4 "context"
5 "sync/atomic"
6 "testing"
7 "time"
8 )
9
10 func TestCatalogMetadataRefreshCoalescesAndJoinsCancellation(t *testing.T) {
11 ctx, cancel := context.WithCancel(t.Context())
12 defer cancel()
13 started := make(chan int, 2)
14 releaseFirst := make(chan struct{})
15 var calls atomic.Int32
16 request, done := startCatalogMetadataRefresh(ctx, func(ctx context.Context) {
17 call := int(calls.Add(1))
18 started <- call
19 if call == 1 {
20 select {
21 case <-releaseFirst:
22 case <-ctx.Done():
23 }
24 } else {
25 <-ctx.Done()
26 }
27 })
28 waitStart := func(want int) {
29 t.Helper()
30 select {
31 case got := <-started:
32 if got != want {
33 t.Fatalf("refresh order %d, want %d", got, want)
34 }
35 case <-time.After(sessionCatalogTestDeadline):
36 t.Fatal("refresh did not start")
37 }
38 }
39 request()
40 waitStart(1)
41 // These requests return while the writer remains blocked. They retain one
42 // trailing refresh so a change arriving during a refresh cannot be lost.
43 for range 1000 {
44 request()
45 }
46 if calls.Load() != 1 {
47 t.Fatal("metadata refreshes overlapped")
48 }
49 close(releaseFirst)
50 waitStart(2)
51 request()
52 cancel()
53 select {
54 case <-done:
55 case <-time.After(sessionCatalogTestDeadline):
56 t.Fatal("metadata worker did not join cancellation")
57 }
58 request()
59 if calls.Load() != 2 {
60 t.Fatal("pending refresh ran after cancellation")
61 }
62 }
63
64 func TestCatalogMetadataRequestsRetainOneWakeupAndUseCurrentLifecycle(t *testing.T) {
65 app := NewApp()
66 old := make(chan struct{}, 1)
67 app.catalogMetadataRequests = old
68 for range 1000 {
69 app.requestSessionCatalogMetadataSync()
70 }
71 if len(old) != 1 {
72 t.Fatal("metadata requests did not coalesce")
73 }
74 <-old
75 current := make(chan struct{}, 1)
76 app.catalogLifecycleMu.Lock()
77 app.catalogMetadataRequests = current
78 app.catalogLifecycleMu.Unlock()
79 app.requestSessionCatalogMetadataSync()
80 if len(old) != 0 || len(current) != 1 {
81 t.Fatal("metadata edit reached an old catalog lifecycle")
82 }
83 <-current
84 app.catalogLifecycleMu.Lock()
85 app.catalogMetadataRequests = nil
86 app.catalogLifecycleMu.Unlock()
87 app.requestSessionCatalogMetadataSync()
88 if len(current) != 0 {
89 t.Fatal("stopped catalog received new metadata work")
90 }
91 }
92
92 lines GO