Repository: codenotary/immudb
File: pkg/integration/replication/synchronous_replication_test.go (Go)

Code before:

import (
	"context"
	"fmt"
	"sync"
	"testing"
	"time"

	"github.com/codenotary/immudb/pkg/api/schema"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/stretchr/testify/suite"
)

type SyncTestSuite struct {
	baseReplicationTestSuite
}

func TestSyncTestSuite(t *testing.T) {
	suite.Run(t, &SyncTestSuite{})
}

func (suite *SyncTestSuite) TestSyncFromMasterToAllFollowers() {
	suite.SetupCluster(2, 2)

	ctx, client, cleanup := suite.ClientForMaser()
	defer cleanup()

	tx1, err := client.Set(ctx, []byte("key1"), []byte("value1"))
	require.NoError(suite.T(), err)

	tx2, err := client.Set(ctx, []byte("key2"), []byte("value2"))
	require.NoError(suite.T(), err)

	for i := 0; i < suite.GetFollowersCount(); i++ {
		suite.Run(fmt.Sprintf("test replica %d", i), func() {
			ctx, client, cleanup := suite.ClientForReplica(i)
			defer cleanup()

			// Tests are flaky because it takes time to commit the
			// precommitted TX, so this function just ensures the state
			// is in sync between master and follower
			suite.WaitForCommittedTx(ctx, client, tx2.Id, time.Second)

			val, err := client.GetAt(ctx, []byte("key1"), tx1.Id)
			require.NoError(suite.T(), err)
			suite.Require().Equal([]byte("value1"), val.Value)

			val, err = client.GetAt(ctx, []byte("key2"), tx2.Id)
			require.NoError(suite.T(), err)
			suite.Require().Equal([]byte("value2"), val.Value)

Review comment (suggestion):
This could be a problem with docker, with so many containers being spawned and closed for a test. If we want many followers, we can create diff test suites right? For ex, for TestMinimumFollowers, we can add a testsuite MinFollowerTestSuite. I think this function would be better in SetupSuite

Code after:

import (
	"context"
	"fmt"
	"sync"
	"testing"
	"time"

	"github.com/codenotary/immudb/pkg/api/schema"
	"github.com/stretchr/testify/require"
	"github.com/stretchr/testify/suite"
)

type SyncTestSuite struct {
	baseReplicationTestSuite
}

func TestSyncTestSuite(t *testing.T) {
	suite.Run(t, &SyncTestSuite{})
}

// this function executes before the test suite begins execution
func (suite *SyncTestSuite) SetupSuite() {
	suite.baseReplicationTestSuite.SetupSuite()
	suite.SetupCluster(2, 2)
}

func (suite *SyncTestSuite) TestSyncFromMasterToAllFollowers() {
	ctx, client, cleanup := suite.ClientForMaser()
	defer cleanup()

	tx1, err := client.Set(ctx, []byte("key1"), []byte("value1"))
	require.NoError(suite.T(), err)

	tx2, err := client.Set(ctx, []byte("key2"), []byte("value2"))
	require.NoError(suite.T(), err)

	for i := 0; i < suite.GetFollowersCount(); i++ {
		suite.Run(fmt.Sprintf("test replica %d", i), func() {
			ctx, client, cleanup := suite.ClientForReplica(i)
			defer cleanup()

			// Tests are flaky because it takes time to commit the
			// precommitted TX, so this function just ensures the state
			// is in sync between master and follower
			suite.WaitForCommittedTx(ctx, client, tx2.Id, time.Second)

			val, err := client.GetAt(ctx, []byte("key1"), tx1.Id)
			require.NoError(suite.T(), err)
			suite.Require().Equal([]byte("value1"), val.Value)