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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package db
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
)
func TestInMemoryRepository(t *testing.T) {
storage := NewRepository[*domain.Sparkle]()
t.Run("Save", func(t *testing.T) {
t.Run("an invalid Sparkle", func(t *testing.T) {
assert.Error(t, storage.Save(t.Context(), &domain.Sparkle{Reason: "because"}))
assert.Equal(t, 0, len(storage.All(t.Context())))
})
t.Run("a valid Sparkle", func(t *testing.T) {
sparkle := &domain.Sparkle{Sparklee: "@tanuki", Reason: "because"}
require.NoError(t, storage.Save(t.Context(), sparkle))
sparkles := storage.All(t.Context())
assert.Equal(t, 1, len(sparkles))
assert.NotEmpty(t, sparkles[0].ID)
assert.Equal(t, "@tanuki", sparkles[0].Sparklee)
assert.Equal(t, "because", sparkles[0].Reason)
})
t.Run("prevents race conditions", func(t *testing.T) {
repository := NewRepository[*domain.Sparkle]()
ctx := context.Background()
numGoroutines := 100
numOperationsPerGoroutine := 50
var wg sync.WaitGroup
errors := make(chan error, numGoroutines*2)
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(workerID int) {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
errors <- assert.AnError
}
}()
for j := 0; j < numOperationsPerGoroutine; j++ {
if err := repository.Save(ctx, &domain.Sparkle{
Sparklee: "@user" + string(rune(workerID)),
Reason: "for running concurrently",
}); err != nil {
errors <- err
return
}
}
}(i)
}
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
errors <- assert.AnError
}
}()
for j := 0; j < numOperationsPerGoroutine; j++ {
_ = repository.All(ctx)
}
}()
}
wg.Wait()
close(errors)
var raceErrors []error
for err := range errors {
raceErrors = append(raceErrors, err)
}
assert.Equal(t, numGoroutines*numOperationsPerGoroutine, len(repository.All(ctx)))
assert.Empty(t, raceErrors)
})
})
t.Run("All", func(t *testing.T) {
repository := NewRepository[*domain.Sparkle]()
require.NoError(t, repository.Save(t.Context(), &domain.Sparkle{
Sparklee: "@tanuki",
Reason: "because",
}))
t.Run("prevents unintended modifications", func(t *testing.T) {
items := repository.All(t.Context())
require.Equal(t, 1, len(items))
sparkle := items[0]
sparkle.Reason = "haha"
assert.Equal(t, "because", repository.All(t.Context())[0].Reason)
})
t.Run("prevents race conditions", func(t *testing.T) {
t.Skip()
})
})
t.Run("Find", func(t *testing.T) {
t.Run("when the entity exists", func(t *testing.T) {
sparkle, err := domain.NewSparkle("@tanuki for testing this func")
require.NoError(t, err)
require.NoError(t, storage.Save(t.Context(), sparkle))
result := storage.Find(t.Context(), sparkle.ID)
require.NotNil(t, result)
require.Equal(t, sparkle, result)
})
t.Run("when the entity does not exist", func(t *testing.T) {
result := storage.Find(t.Context(), "unknown")
require.Nil(t, result)
})
})
}
|