Skip to content

Commit 635a098

Browse files
authored
Merge pull request #52 from alexhornbake/alex/fix_update_cleanup_bug
fix cleanup after update bug and concurrent writes to allSnapshots
2 parents 9ffb2fa + f55a7aa commit 635a098

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

abide.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
var (
1515
args *arguments
1616
allSnapshots snapshots
17+
allSnapMutex sync.Mutex
1718
)
1819

1920
var (
@@ -200,8 +201,18 @@ func getSnapshot(id snapshotID) *snapshot {
200201
return allSnapshots[id]
201202
}
202203

203-
// createSnapshot creates or updates a Snapshot.
204+
// createSnapshot creates a Snapshot.
204205
func createSnapshot(id snapshotID, value string) (*snapshot, error) {
206+
return writeSnapshot(id, value, false)
207+
}
208+
209+
// updateSnapshot creates a Snapshot.
210+
func updateSnapshot(id snapshotID, value string) (*snapshot, error) {
211+
return writeSnapshot(id, value, true)
212+
}
213+
214+
// writeSnapshot creates or updates a Snapshot.
215+
func writeSnapshot(id snapshotID, value string, isUpdate bool) (*snapshot, error) {
205216
if !id.isValid() {
206217
return nil, errInvalidSnapshotID
207218
}
@@ -223,11 +234,15 @@ func createSnapshot(id snapshotID, value string) (*snapshot, error) {
223234
path := filepath.Join(dir, fmt.Sprintf("%s%s", pkg, snapshotExt))
224235

225236
s := &snapshot{
226-
id: id,
227-
value: value,
228-
path: path,
237+
id: id,
238+
value: value,
239+
path: path,
240+
evaluated: isUpdate,
229241
}
242+
243+
allSnapMutex.Lock()
230244
allSnapshots[id] = s
245+
allSnapMutex.Unlock()
231246

232247
err = allSnapshots.save()
233248
if err != nil {

abide_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,46 @@ func TestCleanup(t *testing.T) {
6969
}
7070
}
7171

72+
func TestCleanupUpdate(t *testing.T) {
73+
defer testingCleanup()
74+
75+
// this snapshot is updated, should be evaluated, and not removed
76+
_ = testingSnapshot("1", "A")
77+
t2 := &testing.T{}
78+
createOrUpdateSnapshot(t2, "1", "B")
79+
80+
// this snapshot is never evaluated, and should be removed
81+
_ = testingSnapshot("2", "B")
82+
83+
snapshot := getSnapshot("1")
84+
if snapshot == nil {
85+
t.Fatal("Expected snapshot[1] to exist.")
86+
}
87+
88+
// If shouldUpdate = true and singleRun = false, the snapshot must be removed.
89+
args.shouldUpdate = true
90+
args.singleRun = false
91+
err := Cleanup()
92+
if err != nil {
93+
t.Fatal(err)
94+
}
95+
96+
// call private reloadSnapshots to repeat once-executing function
97+
err = reloadSnapshots()
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
102+
snapshot = getSnapshot("1")
103+
if snapshot == nil {
104+
t.Fatal("Expected snapshot[1] to exist.")
105+
}
106+
snapshot = getSnapshot("2")
107+
if snapshot != nil {
108+
t.Fatal("Expected snapshot[2] to be removed.")
109+
}
110+
}
111+
72112
func TestSnapshotIDIsValid(t *testing.T) {
73113
id := snapshotID("1")
74114
if !id.isValid() {

assert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func createOrUpdateSnapshot(t *testing.T, id, data string) {
160160
if diff != "" {
161161
if snapshot != nil && args.shouldUpdate {
162162
fmt.Printf("Updating snapshot `%s`\n", id)
163-
_, err = createSnapshot(snapshotID(id), data)
163+
_, err = updateSnapshot(snapshotID(id), data)
164164
if err != nil {
165165
t.Fatal(err)
166166
}

0 commit comments

Comments
 (0)