Skip to content

Commit 92233db

Browse files
jkawanJenita
authored andcommitted
test: added unit test for chainsync filter plugin
Signed-off-by: Jenita <jkawan@blinklabs.co>
1 parent 3b61c36 commit 92233db

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

filter/chainsync/plugin_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package chainsync
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/blinklabs-io/adder/event"
8+
"github.com/blinklabs-io/adder/input/chainsync"
9+
"github.com/blinklabs-io/adder/plugin"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestPluginRegistration(t *testing.T) {
14+
// Retrieve the plugin entries
15+
plugins := plugin.GetPlugins(plugin.PluginTypeFilter) // Get all registered plugins
16+
17+
// Find the "chainsync" plugin
18+
var p plugin.Plugin
19+
for _, entry := range plugins {
20+
if entry.Name == "chainsync" {
21+
// Create a new instance of the plugin
22+
p = entry.NewFromOptionsFunc()
23+
break
24+
}
25+
}
26+
27+
// Verify that the plugin was found
28+
assert.NotNil(t, p, "Plugin should be registered")
29+
30+
// Verify that the plugin implements the Plugin interface
31+
_, ok := p.(plugin.Plugin)
32+
assert.True(t, ok, "Plugin should implement the Plugin interface")
33+
}
34+
35+
func TestPluginStartStop(t *testing.T) {
36+
// Create a new plugin instance
37+
p := NewFromCmdlineOptions()
38+
39+
// Start the plugin
40+
err := p.Start()
41+
assert.NoError(t, err, "Plugin should start without errors")
42+
43+
// Stop the plugin
44+
err = p.Stop()
45+
assert.NoError(t, err, "Plugin should stop without errors")
46+
}
47+
48+
func TestPluginChannels(t *testing.T) {
49+
// Create a new plugin instance
50+
p := NewFromCmdlineOptions()
51+
52+
// Verify that the error channel is not nil
53+
assert.NotNil(t, p.ErrorChan(), "Error channel should not be nil")
54+
55+
// Verify that the input channel is not nil
56+
assert.NotNil(t, p.InputChan(), "Input channel should not be nil")
57+
58+
// Verify that the output channel is not nil
59+
assert.NotNil(t, p.OutputChan(), "Output channel should not be nil")
60+
}
61+
62+
func TestPluginEventProcessing(t *testing.T) {
63+
// Create a new plugin instance
64+
p := NewFromCmdlineOptions()
65+
66+
// Start the plugin
67+
err := p.Start()
68+
assert.NoError(t, err, "Plugin should start without errors")
69+
70+
// Create a test event with a TransactionEvent payload
71+
testEvent := event.Event{
72+
Type: "transaction",
73+
Timestamp: time.Now(),
74+
Payload: chainsync.TransactionEvent{},
75+
}
76+
77+
// Send the event to the input channel
78+
p.InputChan() <- testEvent
79+
80+
// Read the event from the output channel
81+
select {
82+
case outputEvent := <-p.OutputChan():
83+
assert.Equal(t, testEvent, outputEvent, "Output event should match input event")
84+
case <-time.After(1 * time.Second):
85+
t.Fatal("Timeout waiting for output event")
86+
}
87+
88+
// Stop the plugin
89+
err = p.Stop()
90+
assert.NoError(t, err, "Plugin should stop without errors")
91+
}

0 commit comments

Comments
 (0)