summaryrefslogtreecommitdiff
path: root/app/db/in_memory_repository_test.go
blob: 5bb220eff50f660668c7bf302c1743bcb4126220 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package db

import (
	"context"
	"sync"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/xlgmokha/x/pkg/event"
	"github.com/xlgmokha/x/pkg/x"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
)

func TestInMemoryRepository(t *testing.T) {
	aggregator := event.New[*domain.Sparkle]()
	storage := NewRepository[*domain.Sparkle](aggregator)

	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("publishes an event", func(t *testing.T) {
			called := false
			var payload *domain.Sparkle

			aggregator.SubscribeTo("after.create", func(item *domain.Sparkle) {
				called = true
				payload = item
			})

			sparkle := &domain.Sparkle{Sparklee: "@tanuki", Reason: "because"}
			require.NoError(t, storage.Save(t.Context(), sparkle))

			require.True(t, called)
			require.NotNil(t, payload)
			assert.Equal(t, sparkle, payload)
		})

		t.Run("prevents race conditions", func(t *testing.T) {
			repository := NewRepository[*domain.Sparkle](aggregator)
			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](aggregator)
		require.NoError(t, repository.Save(t.Context(), &domain.Sparkle{
			Sparklee: "@tanuki",
			Reason:   "because",
		}))

		t.Run("returns all the items", func(t *testing.T) {
			items := repository.All(t.Context())

			require.NotNil(t, items)
			require.Equal(t, 1, len(items))
			assert.Equal(t, "@tanuki", items[0].Sparklee)
			assert.Equal(t, "because", items[0].Reason)
		})
	})

	t.Run("Find", func(t *testing.T) {
		t.Run("when the entity exists", func(t *testing.T) {
			sparkle := x.New[*domain.Sparkle](domain.WithText("@tanuki for testing this func"))
			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)
		})
	})
}