Skip to content

Commit

Permalink
Split tests in two files, use white-box testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjat committed Nov 7, 2023
1 parent beefff5 commit 1c01dc8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
11 changes: 0 additions & 11 deletions cmd/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,3 @@ func Register(parentCmd *cobra.Command) {
analyzeCmd.Flags().StringVar(&configFile, "config", "./config/local.yml", "path to the config.yml file")
parentCmd.AddCommand(analyzeCmd)
}

// For testing purposes only.
type ServiceTester struct {
Service
}

func (a *ServiceTester) SetAnalyzers(fastSyncAnalyzers []SyncedAnalyzer, analyzers []SyncedAnalyzer) {
a.fastSyncAnalyzers = fastSyncAnalyzers
a.analyzers = analyzers
a.logger = log.NewDefaultLogger("analyzer")
}
31 changes: 31 additions & 0 deletions cmd/analyzer/analyzer_migrations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package analyzer_test

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"

cmdAnalyzer "github.com/oasisprotocol/nexus/cmd/analyzer"
"github.com/oasisprotocol/nexus/storage/postgres/testutil"
"github.com/oasisprotocol/nexus/tests"
)

// Relative path to the migrations directory when running tests in this file.
// When running go tests, the working directory is always set to the package directory of the test being run.
const migrationsPath = "file://../../storage/migrations"

func TestMigrations(t *testing.T) {
tests.SkipIfShort(t)
client := testutil.NewTestClient(t)
defer client.Close()

ctx := context.Background()

// Ensure database is empty before running migrations.
require.NoError(t, client.Wipe(ctx), "failed to wipe database")

// Run migrations.
require.NoError(t, cmdAnalyzer.RunMigrations(migrationsPath, os.Getenv("CI_TEST_CONN_STRING")), "failed to run migrations")
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,28 @@
package analyzer_test
package analyzer

import (
"context"
"os"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/oasisprotocol/nexus/analyzer"
cmdAnalyzer "github.com/oasisprotocol/nexus/cmd/analyzer"
"github.com/oasisprotocol/nexus/storage/postgres/testutil"
"github.com/oasisprotocol/nexus/tests"
"github.com/oasisprotocol/nexus/log"
)

// Relative path to the migrations directory when running tests in this file.
// When running go tests, the working directory is always set to the package directory of the test being run.
const migrationsPath = "file://../../storage/migrations"

func TestMigrations(t *testing.T) {
tests.SkipIfShort(t)
client := testutil.NewTestClient(t)
defer client.Close()

ctx := context.Background()

// Ensure database is empty before running migrations.
require.NoError(t, client.Wipe(ctx), "failed to wipe database")

// Run migrations.
require.NoError(t, cmdAnalyzer.RunMigrations(migrationsPath, os.Getenv("CI_TEST_CONN_STRING")), "failed to run migrations")
}

// A trivial analyzer that runs for `duration`, then appends its `name` to `finishLog`.
type DummyAnalyzer struct {
name string
duration time.Duration
finishLog *[]string
}

var finishLogLock = &sync.Mutex{} // for use by all DummyAnalyzer instances

var _ analyzer.Analyzer = (*DummyAnalyzer)(nil)

var finishLogLock = &sync.Mutex{} // for use by all DummyAnalyzer instances

func (a *DummyAnalyzer) Start(ctx context.Context) {
time.Sleep(a.duration)
finishLogLock.Lock()
Expand All @@ -55,8 +34,8 @@ func (a *DummyAnalyzer) Name() string {
return a.name
}

func dummyAnalyzer(syncTag string, name string, duration time.Duration, finishLog *[]string) cmdAnalyzer.SyncedAnalyzer {
return cmdAnalyzer.SyncedAnalyzer{
func dummyAnalyzer(syncTag string, name string, duration time.Duration, finishLog *[]string) SyncedAnalyzer {
return SyncedAnalyzer{
SyncTag: syncTag,
Analyzer: &DummyAnalyzer{
name: name,
Expand All @@ -67,7 +46,7 @@ func dummyAnalyzer(syncTag string, name string, duration time.Duration, finishLo
}

func TestSequencing(t *testing.T) {
s := cmdAnalyzer.ServiceTester{}
// Log of analyzer completions. Each analyzer, when it completes, will append its names to this list.
finishLog := []string{}
// Fast analyzers: Tag "a" finishes after 1 second, tag "b" finishes after 3 seconds.
fastA := dummyAnalyzer("a", "fastA", 1*time.Second, &finishLog)
Expand All @@ -77,10 +56,12 @@ func TestSequencing(t *testing.T) {
slowA := dummyAnalyzer("a", "slowA", 1*time.Second, &finishLog)
slowB := dummyAnalyzer("b", "slowB", 1*time.Second, &finishLog)
slowX := dummyAnalyzer("", "slowX", 0*time.Second, &finishLog)
s.SetAnalyzers(
[]cmdAnalyzer.SyncedAnalyzer{fastA, fastB1, fastB2},
[]cmdAnalyzer.SyncedAnalyzer{slowA, slowB, slowX},
)

s := Service{
fastSyncAnalyzers: []SyncedAnalyzer{fastA, fastB1, fastB2},
analyzers: []SyncedAnalyzer{slowA, slowB, slowX},
logger: log.NewDefaultLogger("analyzer"),
}

s.Start()
require.Equal(t, []string{
Expand Down

0 comments on commit 1c01dc8

Please sign in to comment.