diff --git a/cmd/analyzer/analyzer.go b/cmd/analyzer/analyzer.go index 0b3fc25a7..0327bd6be 100644 --- a/cmd/analyzer/analyzer.go +++ b/cmd/analyzer/analyzer.go @@ -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") -} diff --git a/cmd/analyzer/analyzer_migrations_test.go b/cmd/analyzer/analyzer_migrations_test.go new file mode 100644 index 000000000..6d9ed67d7 --- /dev/null +++ b/cmd/analyzer/analyzer_migrations_test.go @@ -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") +} diff --git a/cmd/analyzer/analyzer_test.go b/cmd/analyzer/analyzer_sequencing_test.go similarity index 63% rename from cmd/analyzer/analyzer_test.go rename to cmd/analyzer/analyzer_sequencing_test.go index 3a9163215..8256e54ea 100644 --- a/cmd/analyzer/analyzer_test.go +++ b/cmd/analyzer/analyzer_sequencing_test.go @@ -1,8 +1,7 @@ -package analyzer_test +package analyzer import ( "context" - "os" "sync" "testing" "time" @@ -10,29 +9,9 @@ import ( "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 @@ -40,10 +19,10 @@ type DummyAnalyzer struct { 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() @@ -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, @@ -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) @@ -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{