From 5277118b70b657c26e777a13b1a651aaa9779f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20SZKIBA?= Date: Wed, 16 Aug 2023 18:12:51 +0200 Subject: [PATCH] test: added some tests --- dashboard/event_test.go | 34 ++++++++++++++++++++++++++++++ dashboard/extension_test.go | 41 +++++++++++++++++++++++++++++++++++++ dashboard/replay_test.go | 30 +++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 dashboard/event_test.go diff --git a/dashboard/event_test.go b/dashboard/event_test.go new file mode 100644 index 0000000..0e1e997 --- /dev/null +++ b/dashboard/event_test.go @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2023 Iván Szkiba +// +// SPDX-License-Identifier: MIT + +package dashboard + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type errorEventListener struct{} + +func (*errorEventListener) onEvent(string, interface{}) {} + +func (*errorEventListener) onStart() error { + return assert.AnError +} + +func (*errorEventListener) onStop() error { + return assert.AnError +} + +func Test_eventSource_error(t *testing.T) { + t.Parallel() + + src := new(eventSource) + + src.addEventListener(new(errorEventListener)) + + assert.Error(t, src.fireStart()) + assert.Error(t, src.fireStop()) +} diff --git a/dashboard/extension_test.go b/dashboard/extension_test.go index 336d0ef..97d352e 100644 --- a/dashboard/extension_test.go +++ b/dashboard/extension_test.go @@ -7,6 +7,7 @@ package dashboard import ( "embed" "net" + "os" "strconv" "strings" "testing" @@ -14,6 +15,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "github.com/szkiba/xk6-dashboard/assets" "go.k6.io/k6/metrics" "go.k6.io/k6/output" ) @@ -79,3 +81,42 @@ func TestExtension(t *testing.T) { assert.NoError(t, ext.Stop()) } + +func TestExtension_report(t *testing.T) { + t.Parallel() + + file, err := os.CreateTemp("", "") + + assert.NoError(t, err) + assert.NoError(t, file.Close()) + + var params output.Params + + params.Logger = logrus.StandardLogger() + params.ConfigArgument = "period=10ms&port=0&report=" + file.Name() + ".gz" + + ext, err := New(params, embed.FS{}, assets.DirBrief()) + + assert.NoError(t, err) + assert.NotNil(t, ext) + + assert.NoError(t, ext.Start()) + + time.Sleep(time.Millisecond) + + go func() { + sample := testSample(t, "foo", metrics.Counter, 1) + + ext.AddMetricSamples(testSampleContainer(t, sample).toArray()) + }() + + assert.NoError(t, ext.Stop()) + + st, err := os.Stat(file.Name() + ".gz") + + assert.NoError(t, err) + + assert.Greater(t, st.Size(), int64(1024)) + + assert.NoError(t, os.Remove(file.Name()+".gz")) +} diff --git a/dashboard/replay_test.go b/dashboard/replay_test.go index c2c6cfd..8d63890 100644 --- a/dashboard/replay_test.go +++ b/dashboard/replay_test.go @@ -6,10 +6,12 @@ package dashboard import ( "embed" + "os" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/szkiba/xk6-dashboard/assets" "go.k6.io/k6/metrics" ) @@ -56,6 +58,34 @@ func Test_replay(t *testing.T) { assert.Equal(t, 5, len(lines)) } +func Test_replay_report(t *testing.T) { + t.Parallel() + + file, err := os.CreateTemp("", "") + + assert.NoError(t, err) + assert.NoError(t, file.Close()) + + opts := &options{ + Port: 0, + Host: "", + Period: time.Second, + Open: false, + Config: "", + Report: file.Name(), + } + + assert.NoError(t, replay(opts, embed.FS{}, assets.DirBrief(), "testdata/result.gz")) + + st, err := os.Stat(file.Name()) + + assert.NoError(t, err) + + assert.Greater(t, st.Size(), int64(1024)) + + assert.NoError(t, os.Remove(file.Name())) +} + func Test_feeder_processMetric(t *testing.T) { t.Parallel()