|
| 1 | +package op |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/OpenListTeam/OpenList/v4/internal/driver" |
| 11 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 12 | + "github.com/OpenListTeam/OpenList/v4/pkg/singleflight" |
| 13 | + "github.com/OpenListTeam/OpenList/v4/pkg/utils" |
| 14 | +) |
| 15 | + |
| 16 | +type linkLifecycleDriver struct { |
| 17 | + model.Storage |
| 18 | + links func() *model.Link |
| 19 | + calls atomic.Int32 |
| 20 | +} |
| 21 | + |
| 22 | +func (d *linkLifecycleDriver) Config() driver.Config { return driver.Config{} } |
| 23 | +func (d *linkLifecycleDriver) GetAddition() driver.Additional { return nil } |
| 24 | +func (d *linkLifecycleDriver) Init(context.Context) error { return nil } |
| 25 | +func (d *linkLifecycleDriver) Drop(context.Context) error { return nil } |
| 26 | +func (d *linkLifecycleDriver) List(context.Context, model.Obj, model.ListArgs) ([]model.Obj, error) { |
| 27 | + return nil, nil |
| 28 | +} |
| 29 | +func (d *linkLifecycleDriver) Get(context.Context, string) (model.Obj, error) { |
| 30 | + return &model.Object{Name: "file", Path: "/file"}, nil |
| 31 | +} |
| 32 | +func (d *linkLifecycleDriver) Link(context.Context, model.Obj, model.LinkArgs) (*model.Link, error) { |
| 33 | + d.calls.Add(1) |
| 34 | + return d.links(), nil |
| 35 | +} |
| 36 | + |
| 37 | +func resetLinkLifecycleState(t *testing.T) { |
| 38 | + t.Helper() |
| 39 | + oldCache := Cache |
| 40 | + Cache, linkG = NewCacheManager(), singleflight.Group[*objWithLink]{} |
| 41 | + t.Cleanup(func() { Cache, linkG = oldCache, singleflight.Group[*objWithLink]{} }) |
| 42 | +} |
| 43 | + |
| 44 | +func acquireTestLink(t *testing.T, d *linkLifecycleDriver) *model.Link { |
| 45 | + t.Helper() |
| 46 | + link, _, err := Link(context.Background(), d, "/file", model.LinkArgs{}) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + return link |
| 51 | +} |
| 52 | + |
| 53 | +func TestLinkLifecycleModes(t *testing.T) { |
| 54 | + t.Run("TTL descriptor remains reusable after close", func(t *testing.T) { |
| 55 | + resetLinkLifecycleState(t) |
| 56 | + ttl := time.Minute |
| 57 | + d := &linkLifecycleDriver{ |
| 58 | + Storage: model.Storage{MountPath: "/ttl"}, |
| 59 | + links: func() *model.Link { return &model.Link{URL: "https://example.test/file", Expiration: &ttl} }, |
| 60 | + } |
| 61 | + |
| 62 | + first := acquireTestLink(t, d) |
| 63 | + _ = first.Close() |
| 64 | + second := acquireTestLink(t, d) |
| 65 | + if second.URL != first.URL || d.calls.Load() != 1 { |
| 66 | + t.Fatalf("TTL link was not reused: calls=%d", d.calls.Load()) |
| 67 | + } |
| 68 | + _ = second.Close() |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("references keep shared resources alive until final close", func(t *testing.T) { |
| 72 | + resetLinkLifecycleState(t) |
| 73 | + var closes atomic.Int32 |
| 74 | + d := &linkLifecycleDriver{ |
| 75 | + Storage: model.Storage{MountPath: "/reference"}, |
| 76 | + links: func() *model.Link { |
| 77 | + return &model.Link{ |
| 78 | + URL: "https://example.test/file", |
| 79 | + SyncClosers: utils.NewSyncClosers(utils.CloseFunc(func() error { closes.Add(1); return nil })), |
| 80 | + RequireReference: true, |
| 81 | + } |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + first := acquireTestLink(t, d) |
| 86 | + second := acquireTestLink(t, d) |
| 87 | + _ = first.Close() |
| 88 | + if closes.Load() != 0 { |
| 89 | + t.Fatal("shared resource closed while another reference was active") |
| 90 | + } |
| 91 | + _ = second.Close() |
| 92 | + if closes.Load() != 1 { |
| 93 | + t.Fatalf("final close count = %d, want 1", closes.Load()) |
| 94 | + } |
| 95 | + third := acquireTestLink(t, d) |
| 96 | + _ = third.Close() |
| 97 | + if d.calls.Load() != 2 || closes.Load() != 2 { |
| 98 | + t.Fatalf("stale link was not replaced: calls=%d closes=%d", d.calls.Load(), closes.Load()) |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("close-invalidated link is reacquired", func(t *testing.T) { |
| 103 | + resetLinkLifecycleState(t) |
| 104 | + d := &linkLifecycleDriver{ |
| 105 | + Storage: model.Storage{MountPath: "/close-invalidated"}, |
| 106 | + links: func() *model.Link { |
| 107 | + return &model.Link{SyncClosers: utils.NewSyncClosers(utils.CloseFunc(func() error { return nil }))} |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + first := acquireTestLink(t, d) |
| 112 | + _ = first.Close() |
| 113 | + second := acquireTestLink(t, d) |
| 114 | + _ = second.Close() |
| 115 | + if d.calls.Load() != 2 { |
| 116 | + t.Fatalf("driver calls = %d, want 2", d.calls.Load()) |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("TTL with owned resources is rejected and released", func(t *testing.T) { |
| 121 | + resetLinkLifecycleState(t) |
| 122 | + ttl := time.Minute |
| 123 | + var closes atomic.Int32 |
| 124 | + d := &linkLifecycleDriver{ |
| 125 | + Storage: model.Storage{MountPath: "/conflict"}, |
| 126 | + links: func() *model.Link { |
| 127 | + return &model.Link{ |
| 128 | + Expiration: &ttl, |
| 129 | + SyncClosers: utils.NewSyncClosers(utils.CloseFunc(func() error { closes.Add(1); return nil })), |
| 130 | + RequireReference: true, |
| 131 | + } |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + _, _, err := Link(context.Background(), d, "/file", model.LinkArgs{}) |
| 136 | + if err == nil || !strings.Contains(err.Error(), "expiration cannot be combined") { |
| 137 | + t.Fatalf("unexpected error: %v", err) |
| 138 | + } |
| 139 | + if closes.Load() != 1 { |
| 140 | + t.Fatalf("rejected link close count = %d, want 1", closes.Load()) |
| 141 | + } |
| 142 | + }) |
| 143 | +} |
0 commit comments