Skip to content

Commit

Permalink
Use config to control noop exporter round start (#1176)
Browse files Browse the repository at this point in the history
* Use config to control noop exporter round start
  • Loading branch information
Eric-Warehime authored Aug 12, 2022
1 parent 3d2720c commit 34986db
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
14 changes: 10 additions & 4 deletions exporters/noop/noop_exporter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package noop

import (
"fmt"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/indexer/data"
"github.com/algorand/indexer/exporters"
"github.com/algorand/indexer/plugins"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

// `noopExporter`s will function without ever erroring. This means they will also process out of order blocks
Expand All @@ -14,7 +16,7 @@ import (
// The `noopExporter` will maintain `Round` state according to the round of the last block it processed.
type noopExporter struct {
round uint64
cfg plugins.PluginConfig
cfg ExporterConfig
}

var noopExporterMetadata exporters.ExporterMetadata = exporters.ExporterMetadata{
Expand All @@ -30,20 +32,24 @@ type Constructor struct{}
func (c *Constructor) New() exporters.Exporter {
return &noopExporter{
round: 0,
cfg: "",
}
}

func (exp *noopExporter) Metadata() exporters.ExporterMetadata {
return noopExporterMetadata
}

func (exp *noopExporter) Init(_ plugins.PluginConfig, _ *logrus.Logger) error {
func (exp *noopExporter) Init(cfg plugins.PluginConfig, _ *logrus.Logger) error {
if err := yaml.Unmarshal([]byte(cfg), &exp.cfg); err != nil {
return fmt.Errorf("init failure in unmarshalConfig: %v", err)
}
exp.round = exp.cfg.Round
return nil
}

func (exp *noopExporter) Config() plugins.PluginConfig {
return exp.cfg
ret, _ := yaml.Marshal(exp.cfg)
return plugins.PluginConfig(ret)
}

func (exp *noopExporter) Close() error {
Expand Down
7 changes: 7 additions & 0 deletions exporters/noop/noop_exporter_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package noop

// ExporterConfig specific to the noop exporter
type ExporterConfig struct {
// Optionally specify the round to start on
Round uint64 `yaml:"round"`
}
22 changes: 18 additions & 4 deletions exporters/noop/noop_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/algorand/indexer/plugins"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)

Expand All @@ -30,29 +31,42 @@ func TestExporterMetadata(t *testing.T) {
assert.Equal(t, noopExporterMetadata.ExpDeprecated, meta.Deprecated())
}

func TestExporterConnect(t *testing.T) {
func TestExporterInit(t *testing.T) {
assert.NoError(t, ne.Init("", nil))
}

func TestExporterConfig(t *testing.T) {
assert.Equal(t, plugins.PluginConfig(""), ne.Config())
defaultConfig := &ExporterConfig{}
expected, err := yaml.Marshal(defaultConfig)
if err != nil {
t.Fatalf("unable to Marshal default noop.ExporterConfig: %v", err)
}
assert.NoError(t, ne.Init("", nil))
assert.Equal(t, plugins.PluginConfig(expected), ne.Config())
}

func TestExporterDisconnect(t *testing.T) {
func TestExporterClose(t *testing.T) {
assert.NoError(t, ne.Close())
}

func TestExporterHandleGenesis(t *testing.T) {
assert.NoError(t, ne.HandleGenesis(bookkeeping.Genesis{}))
}

func TestExporterStartRound(t *testing.T) {
assert.NoError(t, ne.Init("", nil))
assert.Equal(t, uint64(0), ne.Round())
assert.NoError(t, ne.Init("round: 55", nil))
assert.Equal(t, uint64(55), ne.Round())

}

func TestExporterRoundReceive(t *testing.T) {
eData := data.BlockData{
BlockHeader: bookkeeping.BlockHeader{
Round: 5,
},
}
assert.Equal(t, uint64(0), ne.Round())
assert.NoError(t, ne.Receive(eData))
assert.Equal(t, uint64(6), ne.Round())
}

0 comments on commit 34986db

Please sign in to comment.