Repository navigation
Expand file tree
/
Copy pathpins_test.go
More file actions
116 lines (102 loc) · 3.66 KB
/
Copy pathpins_test.go
File metadata and controls
116 lines (102 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package published
import (
"context"
"errors"
"reflect"
"sync"
"testing"
"time"
"github.com/calypr/loom/internal/dataset"
"github.com/calypr/loom/internal/dataframe/publication"
)
type pinTestCatalog struct {
publication.BundleCatalog
mu sync.Mutex
acquired []string
released []string
failedID string
loseRenewal bool
}
func (c *pinTestCatalog) AcquireExecutionReadPin(_ context.Context, executionID, _ string, _ time.Time) (bool, error) {
c.mu.Lock()
defer c.mu.Unlock()
if executionID == c.failedID {
return false, nil
}
c.acquired = append(c.acquired, executionID)
return true, nil
}
func (c *pinTestCatalog) RenewExecutionReadPin(context.Context, string, string, time.Time) (bool, error) {
return !c.loseRenewal, nil
}
func (c *pinTestCatalog) ReleaseExecutionReadPin(_ context.Context, executionID, _ string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.released = append(c.released, executionID)
return nil
}
func (*pinTestCatalog) ClaimExecutionCleanup(context.Context, string, string) (bool, error) {
return false, nil
}
func (*pinTestCatalog) RenewExecutionCleanup(context.Context, string, string, time.Time) (bool, error) {
return false, nil
}
func (*pinTestCatalog) ReleaseExecutionCleanup(context.Context, string, string) error { return nil }
func TestWithExecutionReadPinsPinsEveryExactRevisionAndReleases(t *testing.T) {
catalog := &pinTestCatalog{}
reader := &Reader{Catalog: catalog}
called := false
err := reader.WithExecutionReadPins(context.Background(), []string{"execution-b", "execution-a", "execution-b"}, func(context.Context) error {
called = true
catalog.mu.Lock()
defer catalog.mu.Unlock()
if !reflect.DeepEqual(catalog.acquired, []string{"execution-a", "execution-b"}) {
t.Fatalf("acquired pins inside visit = %#v", catalog.acquired)
}
return nil
})
if err != nil {
t.Fatalf("WithExecutionReadPins() error = %v", err)
}
if !called {
t.Fatal("pin visitor was not called")
}
if !reflect.DeepEqual(catalog.released, []string{"execution-a", "execution-b"}) {
t.Fatalf("released pins = %#v", catalog.released)
}
}
func TestWithExecutionReadPinsReleasesPartialAcquisitionOnFailure(t *testing.T) {
catalog := &pinTestCatalog{failedID: "execution-b"}
reader := &Reader{Catalog: catalog}
called := false
err := reader.WithExecutionReadPins(context.Background(), []string{"execution-a", "execution-b"}, func(context.Context) error {
called = true
return nil
})
if !errors.Is(err, publication.ErrExecutionReadPinLost) {
t.Fatalf("WithExecutionReadPins() error = %v, want lost-pin error", err)
}
if called {
t.Fatal("visitor ran after an input revision could not be pinned")
}
if !reflect.DeepEqual(catalog.released, []string{"execution-a"}) {
t.Fatalf("released partial pins = %#v", catalog.released)
}
}
func TestWithExecutionReadPinsCancelsVisitorOnRenewalLoss(t *testing.T) {
catalog := &pinTestCatalog{loseRenewal: true}
reader := &Reader{Catalog: catalog}
err := reader.withExecutionReadPins(context.Background(), []string{"execution-a"}, 10*time.Millisecond, time.Millisecond, func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
if !errors.Is(err, publication.ErrExecutionReadPinLost) {
t.Fatalf("WithExecutionReadPins() error = %v, want lost-pin error", err)
}
if !reflect.DeepEqual(catalog.released, []string{"execution-a"}) {
t.Fatalf("released after renewal loss = %#v", catalog.released)
}
}
func (*pinTestCatalog) FindExecutionBySelector(context.Context, string, string, dataset.DataframeSelector) (publication.BundleExecution, publication.BundleOutputRecord, error) {
return publication.BundleExecution{}, publication.BundleOutputRecord{}, publication.ErrBundleNotFound
}