Skip to content

Commit

Permalink
test: added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Aug 16, 2023
1 parent 7d89d45 commit 5277118
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
34 changes: 34 additions & 0 deletions dashboard/event_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
41 changes: 41 additions & 0 deletions dashboard/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ package dashboard
import (
"embed"
"net"
"os"
"strconv"
"strings"
"testing"
"time"

"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"
)
Expand Down Expand Up @@ -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"))
}
30 changes: 30 additions & 0 deletions dashboard/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 5277118

Please sign in to comment.