Skip to content

Commit ddb2e6c

Browse files
Added more tests for instrance params operations, according to suggestions
1 parent a5676d7 commit ddb2e6c

File tree

1 file changed

+76
-21
lines changed

1 file changed

+76
-21
lines changed

storage/operation/instance_params_test.go

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,85 @@ import (
1616
// TestInstanceParams_InsertRetrieve verifies that InstanceParams can be
1717
// correctly stored and retrieved from the database as a single encoded
1818
// structure.
19+
// Test cases:
20+
// 1. InstanceParams can be inserted and retrieved successfully.
21+
// 2. Overwrite attempts return storage.ErrAlreadyExists and do not change the
22+
// persisted value.
23+
// 3. Writes without holding LockBootstrapping are denied.
1924
func TestInstanceParams_InsertRetrieve(t *testing.T) {
20-
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
21-
enc, err := datastore.NewVersionedInstanceParams(
22-
datastore.DefaultInstanceParamsVersion,
23-
unittest.IdentifierFixture(),
24-
unittest.IdentifierFixture(),
25-
unittest.IdentifierFixture(),
26-
)
27-
require.NoError(t, err)
28-
29-
lockManager := storage.NewTestingLockManager()
30-
lctx := lockManager.NewContext()
31-
require.NoError(t, lctx.AcquireLock(storage.LockBootstrapping))
32-
defer lctx.Release()
33-
34-
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
35-
return operation.InsertInstanceParams(lctx, rw, *enc)
25+
lockManager := storage.NewTestingLockManager()
26+
enc, err := datastore.NewVersionedInstanceParams(
27+
datastore.DefaultInstanceParamsVersion,
28+
unittest.IdentifierFixture(),
29+
unittest.IdentifierFixture(),
30+
unittest.IdentifierFixture(),
31+
)
32+
require.NoError(t, err)
33+
34+
t.Run("happy path: insert and retrieve", func(t *testing.T) {
35+
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
36+
lctx := lockManager.NewContext()
37+
require.NoError(t, lctx.AcquireLock(storage.LockBootstrapping))
38+
defer lctx.Release()
39+
40+
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
41+
return operation.InsertInstanceParams(lctx, rw, *enc)
42+
})
43+
require.NoError(t, err)
44+
45+
var actual flow.VersionedInstanceParams
46+
err = operation.RetrieveInstanceParams(db.Reader(), &actual)
47+
require.NoError(t, err)
48+
require.Equal(t, enc, &actual)
3649
})
37-
require.NoError(t, err)
50+
})
51+
52+
t.Run("overwrite returns ErrAlreadyExists", func(t *testing.T) {
53+
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
54+
lctx := lockManager.NewContext()
55+
require.NoError(t, lctx.AcquireLock(storage.LockBootstrapping))
56+
57+
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
58+
return operation.InsertInstanceParams(lctx, rw, *enc)
59+
})
60+
require.NoError(t, err)
61+
lctx.Release()
62+
63+
// try to overwrite with different params
64+
enc2, err := datastore.NewVersionedInstanceParams(
65+
datastore.DefaultInstanceParamsVersion,
66+
unittest.IdentifierFixture(),
67+
unittest.IdentifierFixture(),
68+
unittest.IdentifierFixture(),
69+
)
70+
require.NoError(t, err)
3871

39-
var actual flow.VersionedInstanceParams
40-
err = operation.RetrieveInstanceParams(db.Reader(), &actual)
41-
require.NoError(t, err)
72+
lctx2 := lockManager.NewContext()
73+
require.NoError(t, lctx2.AcquireLock(storage.LockBootstrapping))
74+
defer lctx2.Release()
4275

43-
require.Equal(t, enc, &actual)
76+
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
77+
return operation.InsertInstanceParams(lctx2, rw, *enc2)
78+
})
79+
require.ErrorIs(t, err, storage.ErrAlreadyExists)
80+
81+
// DB must still contain original value
82+
var check flow.VersionedInstanceParams
83+
err = operation.RetrieveInstanceParams(db.Reader(), &check)
84+
require.NoError(t, err)
85+
require.Equal(t, enc, &check)
86+
})
87+
})
88+
89+
t.Run("insert without required lock", func(t *testing.T) {
90+
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
91+
lctx := lockManager.NewContext()
92+
defer lctx.Release()
93+
94+
err = db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
95+
return operation.InsertInstanceParams(lctx, rw, *enc)
96+
})
97+
require.ErrorContains(t, err, storage.LockBootstrapping)
98+
})
4499
})
45100
}

0 commit comments

Comments
 (0)