-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests and changed some error messages for sync algod
- Loading branch information
1 parent
380e708
commit ae09ea0
Showing
3 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
conduit/plugins/importers/syncalgod/syncalgod_importer_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package syncalgod | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/algorand/indexer/conduit/plugins" | ||
"github.com/algorand/indexer/conduit/plugins/importers" | ||
"github.com/algorand/indexer/util/test" | ||
) | ||
|
||
var ( | ||
logger *logrus.Logger | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
testImporter importers.Importer | ||
) | ||
|
||
func init() { | ||
logger = logrus.New() | ||
logger.SetOutput(os.Stdout) | ||
logger.SetLevel(logrus.InfoLevel) | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
} | ||
|
||
// TestImporterMetadata tests that metadata is correctly set | ||
func TestImporterMetadata(t *testing.T) { | ||
testImporter = New() | ||
metadata := testImporter.Metadata() | ||
assert.Equal(t, metadata.Name, syncAlgodModeImporterMetadata.Name) | ||
assert.Equal(t, metadata.Description, syncAlgodModeImporterMetadata.Description) | ||
assert.Equal(t, metadata.Deprecated, syncAlgodModeImporterMetadata.Deprecated) | ||
} | ||
|
||
// TestCloseSuccess tests that closing results in no error | ||
func TestCloseSuccess(t *testing.T) { | ||
ts := test.NewAlgodServer(test.GenesisResponder) | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("netaddr: "+ts.URL), logger) | ||
assert.NoError(t, err) | ||
err = testImporter.Close() | ||
assert.NoError(t, err) | ||
} | ||
|
||
// TestInitSuccess tests that initializing results in no error | ||
func TestInitSuccess(t *testing.T) { | ||
ts := test.NewAlgodServer(test.GenesisResponder) | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("netaddr: "+ts.URL), logger) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, testImporter, nil) | ||
testImporter.Close() | ||
} | ||
|
||
// TestInitUnmarshalFailure tests config marshaling failures | ||
func TestInitUnmarshalFailure(t *testing.T) { | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("`"), logger) | ||
assert.Error(t, err) | ||
assert.ErrorContains(t, err, "connect failure in unmarshalConfig") | ||
testImporter.Close() | ||
} | ||
|
||
// TestConfigDefault tests that configuration is correct by default | ||
func TestConfigDefault(t *testing.T) { | ||
testImporter = New() | ||
expected, err := yaml.Marshal(&Config{}) | ||
if err != nil { | ||
t.Fatalf("unable to Marshal default algodimporter.Config: %v", err) | ||
} | ||
assert.Equal(t, string(expected), testImporter.Config()) | ||
} | ||
|
||
// TestWaitForBlockBlockFailure tests that GetBlock results in a failure | ||
func TestWaitForBlockBlockFailure(t *testing.T) { | ||
ts := test.NewAlgodServer(test.GenesisResponder) | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("netaddr: "+ts.URL), logger) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, testImporter, nil) | ||
|
||
blk, err := testImporter.GetBlock(uint64(10)) | ||
assert.Error(t, err) | ||
assert.True(t, blk.Empty()) | ||
} | ||
|
||
// TestGetBlockSuccess tests that GetBlock results in success | ||
func TestGetBlockSuccess(t *testing.T) { | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
ts := test.NewAlgodServer( | ||
test.GenesisResponder, | ||
test.BlockResponder, | ||
test.BlockAfterResponder, test.LedgerStateDeltaResponder) | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("netaddr: "+ts.URL), logger) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, testImporter, nil) | ||
|
||
downloadedBlk, err := testImporter.GetBlock(uint64(10)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, downloadedBlk.Round(), uint64(10)) | ||
assert.True(t, downloadedBlk.Empty()) | ||
cancel() | ||
} | ||
|
||
// TestGetBlockContextCancelled results in an error if the context is cancelled | ||
func TestGetBlockContextCancelled(t *testing.T) { | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
ts := test.NewAlgodServer( | ||
test.GenesisResponder, | ||
test.BlockResponder, | ||
test.BlockAfterResponder, test.LedgerStateDeltaResponder) | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("netaddr: "+ts.URL), logger) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, testImporter, nil) | ||
|
||
cancel() | ||
_, err = testImporter.GetBlock(uint64(10)) | ||
assert.Error(t, err) | ||
} | ||
|
||
// TestGetBlockFailureBlockResponder tests that GetBlock results in an error due to a lack of block responsiveness | ||
func TestGetBlockFailureBlockResponder(t *testing.T) { | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
ts := test.NewAlgodServer( | ||
test.GenesisResponder, | ||
test.BlockAfterResponder, test.LedgerStateDeltaResponder) | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("netaddr: "+ts.URL), logger) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, testImporter, nil) | ||
|
||
_, err = testImporter.GetBlock(uint64(10)) | ||
assert.Error(t, err) | ||
cancel() | ||
} | ||
|
||
// TestGetBlockFailureLedgerStateDeltaResponder tests that GetBlock results in an error due to a lack of ledger state delta | ||
func TestGetBlockFailureLedgerStateDeltaResponder(t *testing.T) { | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
ts := test.NewAlgodServer( | ||
test.GenesisResponder, | ||
test.BlockResponder, | ||
test.BlockAfterResponder) | ||
testImporter = New() | ||
_, err := testImporter.Init(ctx, plugins.MakePluginConfig("netaddr: "+ts.URL), logger) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, testImporter, nil) | ||
|
||
_, err = testImporter.GetBlock(uint64(10)) | ||
assert.Error(t, err) | ||
cancel() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters