-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add app test * chore: add test reporter test * chore: fixed linting
- Loading branch information
1 parent
1be8af7
commit 5a648d6
Showing
6 changed files
with
248 additions
and
44 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
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,147 @@ | ||
package pkg | ||
|
||
import ( | ||
"errors" | ||
configPkg "main/pkg/config" | ||
configTypes "main/pkg/config/types" | ||
filtererPkg "main/pkg/filterer" | ||
"main/pkg/fs" | ||
loggerPkg "main/pkg/logger" | ||
metricsPkg "main/pkg/metrics" | ||
reportersPkg "main/pkg/reporters" | ||
"main/pkg/types" | ||
"net/http" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewAppInvalidToml(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer func() { | ||
if r := recover(); r == nil { | ||
require.Fail(t, "Expected to have a panic here!") | ||
} | ||
}() | ||
|
||
NewApp(&fs.MockFs{}, "invalid-toml.toml", "1.2.3") | ||
} | ||
|
||
func TestNewAppValidTomlWithWarnings(t *testing.T) { | ||
t.Parallel() | ||
|
||
app := NewApp(&fs.MockFs{}, "valid-unused-chain.toml", "1.2.3") | ||
require.NotNil(t, app) | ||
} | ||
|
||
func TestNewAppValidToml(t *testing.T) { | ||
t.Parallel() | ||
|
||
app := NewApp(&fs.MockFs{}, "valid.toml", "1.2.3") | ||
require.NotNil(t, app) | ||
} | ||
|
||
//nolint:paralleltest // disabled due to httpmock usage | ||
func TestAppStart(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("GET", "http://localhost:9580/healthcheck", httpmock.InitialTransport.RoundTrip) | ||
httpmock.RegisterResponder("GET", "http://localhost:9580/metrics", httpmock.InitialTransport.RoundTrip) | ||
|
||
app := NewApp(&fs.MockFs{}, "valid.toml", "1.2.3") | ||
require.NotNil(t, app) | ||
|
||
go app.Start() | ||
|
||
for { | ||
request, err := http.Get("http://localhost:9580/healthcheck") | ||
if request != nil && request.Body != nil { | ||
_ = request.Body.Close() | ||
} | ||
if err == nil { | ||
break | ||
} | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
} | ||
|
||
app.NodesManager.Channel <- types.Report{ | ||
Chain: &configTypes.Chain{Name: "chain"}, | ||
Node: "node", | ||
Reportable: &types.NodeConnectError{ | ||
Error: errors.New("some error"), | ||
}, | ||
} | ||
|
||
app.QuitChannel <- syscall.SIGTERM | ||
} | ||
|
||
func TestAppProcessReport(t *testing.T) { | ||
t.Parallel() | ||
|
||
config := &configPkg.AppConfig{ | ||
Chains: configTypes.Chains{ | ||
{Name: "chain"}, | ||
}, | ||
Subscriptions: configTypes.Subscriptions{ | ||
{ | ||
Name: "subscription", | ||
Reporter: "test-reporter", | ||
ChainSubscriptions: configTypes.ChainSubscriptions{ | ||
{ | ||
Chain: "chain", | ||
Filters: configTypes.Filters{}, | ||
LogNodeErrors: true, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "subscription", | ||
Reporter: "test-reporter-2", | ||
ChainSubscriptions: configTypes.ChainSubscriptions{ | ||
{ | ||
Chain: "chain", | ||
Filters: configTypes.Filters{}, | ||
LogNodeErrors: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Reporters: configTypes.Reporters{ | ||
{ | ||
Name: "test-reporter", | ||
}, | ||
{ | ||
Name: "test-reporter-2", | ||
}, | ||
}, | ||
} | ||
|
||
logger := loggerPkg.GetNopLogger() | ||
metricsManager := metricsPkg.NewManager(logger, configPkg.MetricsConfig{Enabled: true}) | ||
filterer := filtererPkg.NewFilterer(logger, config, metricsManager) | ||
|
||
app := &App{ | ||
Reporters: reportersPkg.Reporters{ | ||
&reportersPkg.TestReporter{ReporterName: "test-reporter"}, | ||
&reportersPkg.TestReporter{ReporterName: "test-reporter-2", FailToSend: true}, | ||
}, | ||
Filterer: filterer, | ||
MetricsManager: metricsManager, | ||
} | ||
|
||
report := types.Report{ | ||
Chain: &configTypes.Chain{Name: "chain"}, | ||
Node: "node", | ||
Reportable: &types.NodeConnectError{ | ||
Error: errors.New("some error"), | ||
}, | ||
} | ||
|
||
app.ProcessReport(report) | ||
} |
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
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,32 @@ | ||
package reporters | ||
|
||
import ( | ||
"errors" | ||
"main/pkg/constants" | ||
"main/pkg/types" | ||
) | ||
|
||
type TestReporter struct { | ||
FailToSend bool | ||
ReporterName string | ||
} | ||
|
||
func (r *TestReporter) Init() { | ||
|
||
} | ||
|
||
func (r *TestReporter) Name() string { | ||
return r.ReporterName | ||
} | ||
|
||
func (r *TestReporter) Type() string { | ||
return constants.ReporterTypeTelegram | ||
} | ||
|
||
func (r *TestReporter) Send(report types.Report) error { | ||
if r.FailToSend { | ||
return errors.New("send error") | ||
} | ||
|
||
return nil | ||
} |
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,16 @@ | ||
package reporters | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTestReporter(t *testing.T) { | ||
t.Parallel() | ||
|
||
reporter := &TestReporter{ReporterName: "test"} | ||
reporter.Init() | ||
assert.Equal(t, "test", reporter.Name()) | ||
assert.Equal(t, "telegram", reporter.Type()) | ||
} |
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