diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2021-05-18 20:20:15 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-09-02 08:23:53 +0300 |
commit | b32b9d8fe4725b4b06ab80ad28402cd4562421e7 (patch) | |
tree | 745f1cbc780637d8f6e1f83d0947ee66d5d6b6fe | |
parent | 005b5f934cb53acaf2ba340b6bf9aa5ded7edec9 (diff) |
Support multiple subtransactions in transaction tests
TestStreamDirectorMutator_Transaction currently only supports voting
on a single subtransaction in the tests. As we'll soon need the ability
to test whether the primary became dirty due to a committed subtransaction,
let's expand the tests to support multiple subtransactions.
(cherry picked from commit 49a6e43808dc6e778d4b2d312b61d41eba4f2ea8)
-rw-r--r-- | internal/praefect/coordinator_pg_test.go | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/internal/praefect/coordinator_pg_test.go b/internal/praefect/coordinator_pg_test.go index 730090e24..78dbb6604 100644 --- a/internal/praefect/coordinator_pg_test.go +++ b/internal/praefect/coordinator_pg_test.go @@ -32,10 +32,14 @@ func getDB(t *testing.T) glsql.DB { } func TestStreamDirectorMutator_Transaction(t *testing.T) { + type subtransactions []struct { + vote string + shouldSucceed bool + } + type node struct { primary bool - vote string - shouldSucceed bool + subtransactions subtransactions shouldGetRepl bool shouldParticipate bool generation int @@ -49,39 +53,39 @@ func TestStreamDirectorMutator_Transaction(t *testing.T) { { desc: "successful vote should not create replication jobs", nodes: []node{ - {primary: true, vote: "foobar", shouldSucceed: true, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, - {primary: false, vote: "foobar", shouldSucceed: true, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, - {primary: false, vote: "foobar", shouldSucceed: true, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, + {primary: true, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, + {primary: false, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, + {primary: false, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, }, }, { desc: "failing vote should not create replication jobs", nodes: []node{ - {primary: true, vote: "foo", shouldSucceed: false, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, - {primary: false, vote: "qux", shouldSucceed: false, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, - {primary: false, vote: "bar", shouldSucceed: false, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, + {primary: true, subtransactions: subtransactions{{vote: "foo", shouldSucceed: false}}, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, + {primary: false, subtransactions: subtransactions{{vote: "qux", shouldSucceed: false}}, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, + {primary: false, subtransactions: subtransactions{{vote: "bar", shouldSucceed: false}}, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, }, }, { desc: "primary should reach quorum with disagreeing secondary", nodes: []node{ - {primary: true, vote: "foobar", shouldSucceed: true, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, - {primary: false, vote: "barfoo", shouldSucceed: false, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, + {primary: true, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, + {primary: false, subtransactions: subtransactions{{vote: "barfoo", shouldSucceed: false}}, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, }, }, { desc: "quorum should create replication jobs for disagreeing node", nodes: []node{ - {primary: true, vote: "foobar", shouldSucceed: true, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, - {primary: false, vote: "foobar", shouldSucceed: true, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, - {primary: false, vote: "barfoo", shouldSucceed: false, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, + {primary: true, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, + {primary: false, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldGetRepl: false, shouldParticipate: true, expectedGeneration: 1}, + {primary: false, subtransactions: subtransactions{{vote: "barfoo", shouldSucceed: false}}, shouldGetRepl: true, shouldParticipate: true, expectedGeneration: 0}, }, }, { desc: "only consistent secondaries should participate", nodes: []node{ - {primary: true, vote: "foobar", shouldSucceed: true, shouldParticipate: true, generation: 1, expectedGeneration: 2}, - {primary: false, vote: "foobar", shouldSucceed: true, shouldParticipate: true, generation: 1, expectedGeneration: 2}, + {primary: true, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldParticipate: true, generation: 1, expectedGeneration: 2}, + {primary: false, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldParticipate: true, generation: 1, expectedGeneration: 2}, {shouldParticipate: false, shouldGetRepl: true, generation: 0, expectedGeneration: 0}, {shouldParticipate: false, shouldGetRepl: true, generation: datastore.GenerationUnknown, expectedGeneration: datastore.GenerationUnknown}, }, @@ -89,7 +93,7 @@ func TestStreamDirectorMutator_Transaction(t *testing.T) { { desc: "secondaries should not participate when primary's generation is unknown", nodes: []node{ - {primary: true, vote: "foobar", shouldSucceed: true, shouldParticipate: true, generation: datastore.GenerationUnknown, expectedGeneration: 0}, + {primary: true, subtransactions: subtransactions{{vote: "foobar", shouldSucceed: true}}, shouldParticipate: true, generation: datastore.GenerationUnknown, expectedGeneration: 0}, {shouldParticipate: false, shouldGetRepl: true, generation: datastore.GenerationUnknown, expectedGeneration: datastore.GenerationUnknown}, }, }, @@ -217,12 +221,18 @@ func TestStreamDirectorMutator_Transaction(t *testing.T) { go func() { defer voterWaitGroup.Done() - vote := voting.VoteFromData([]byte(node.vote)) - err := txMgr.VoteTransaction(ctx, transaction.ID, fmt.Sprintf("node-%d", i), vote) - if node.shouldSucceed { - assert.NoError(t, err) - } else { - assert.True(t, errors.Is(err, transactions.ErrTransactionFailed)) + for _, subtransaction := range node.subtransactions { + vote := voting.VoteFromData([]byte(subtransaction.vote)) + err := txMgr.VoteTransaction(ctx, transaction.ID, fmt.Sprintf("node-%d", i), vote) + if subtransaction.shouldSucceed { + if !assert.NoError(t, err) { + break + } + } else { + if !assert.True(t, errors.Is(err, transactions.ErrTransactionFailed)) { + break + } + } } }() } |