Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing tests for go/vt/vtorc/collection #15070

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go/vt/vtorc/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ func (c *Collection) removeBefore(t time.Time) error {
// get the interval we need.
if first == len(c.collection) {
c.collection = nil // remove all entries
} else if first != -1 {
c.collection = c.collection[first:]
} else {
c.collection = c.collection[first+1:]
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
}
return nil // no errors
}
Expand Down
115 changes: 115 additions & 0 deletions go/vt/vtorc/collection/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package collection
import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

var randomString = []string{
Expand All @@ -28,6 +30,7 @@ var randomString = []string{

// some random base timestamp
var ts = time.Date(2016, 12, 27, 13, 36, 40, 0, time.Local)
var ts2 = ts.AddDate(-1, 0, 0)

// TestCreateOrReturn tests the creation of a named Collection
func TestCreateOrReturnCollection(t *testing.T) {
Expand Down Expand Up @@ -87,6 +90,13 @@ func (tm *testMetric) When() time.Time {
return ts
}

type testMetric2 struct {
}

func (tm *testMetric2) When() time.Time {
return ts2
}

// check that Append() works as expected
func TestAppend(t *testing.T) {
c := &Collection{}
Expand All @@ -101,4 +111,109 @@ func TestAppend(t *testing.T) {
t.Errorf("TestExpirePeriod: len(Metrics) = %d, expecting %d", len(c.Metrics()), v)
}
}

// Test for nil metric
err := c.Append(nil)
assert.Error(t, err)
assert.Equal(t, err.Error(), "Collection.Append: m == nil")
}

func TestNilCollection(t *testing.T) {
var c *Collection

metrics := c.Metrics()
assert.Nil(t, metrics)

err := c.Append(nil)
assert.Error(t, err)
assert.Equal(t, err.Error(), "Collection.Append: c == nil")

err = c.removeBefore(ts)
assert.Error(t, err)
assert.Equal(t, err.Error(), "Collection.removeBefore: c == nil")

// Should not throw any error for nil Collection
c.StartAutoExpiration()
c.StopAutoExpiration()
}

func TestStopAutoExpiration(t *testing.T) {
// Clear Collection map
namedCollection = make(map[string]*Collection)

name := randomString[0]
c := CreateOrReturnCollection(name)

c.StopAutoExpiration()
assert.False(t, c.monitoring)

// Test when c.monitoring == true before calling StartAutoExpiration
c.monitoring = true
c.StartAutoExpiration()
assert.True(t, c.monitoring)
}

func TestSince(t *testing.T) {
// Clear Collection map
namedCollection = make(map[string]*Collection)

name := randomString[0]

var c *Collection
metrics, err := c.Since(ts)

assert.Nil(t, metrics)
assert.Error(t, err)
assert.Equal(t, err.Error(), "Collection.Since: c == nil")

c = CreateOrReturnCollection(name)
metrics, err = c.Since(ts)
assert.Nil(t, metrics)
assert.Nil(t, err)

tm := &testMetric{}
tm2 := &testMetric2{}
_ = c.Append(tm2)
_ = c.Append(tm)

metrics, err = c.Since(ts2)
assert.Equal(t, []Metric{tm2, tm}, metrics)
assert.Nil(t, err)

metrics, err = c.Since(ts)
assert.Equal(t, []Metric{tm}, metrics)
assert.Nil(t, err)
}

func TestRemoveBefore(t *testing.T) {
// Clear Collection map
namedCollection = make(map[string]*Collection)
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved

name := randomString[0]
c := CreateOrReturnCollection(name)

tm := &testMetric{}
tm2 := &testMetric2{}

err := c.Append(tm2)
assert.Nil(t, err)

err = c.Append(tm)
assert.Nil(t, err)

err = c.removeBefore(ts)
assert.NoError(t, err)
assert.Equal(t, []Metric{tm}, c.collection)

ts3 := ts.AddDate(1, 0, 0)
err = c.removeBefore(ts3)
assert.NoError(t, err)
assert.Nil(t, c.collection)

name = randomString[1]
c = CreateOrReturnCollection(name)

err = c.removeBefore(ts)
assert.NoError(t, err)
assert.Equal(t, []Metric(nil), c.collection)
}
Loading