Refactor all tests to use the MemoryLeakTestSuite.
This commit is contained in:
@ -2,9 +2,10 @@ package logging
|
||||
|
||||
import (
|
||||
"github.com/openHPI/poseidon/pkg/dto"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@ -16,34 +17,42 @@ func mockHTTPStatusHandler(status int) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTPMiddlewareWarnsWhenInternalServerError(t *testing.T) {
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestHTTPMiddlewareWarnsWhenInternalServerError() {
|
||||
var hook *test.Hook
|
||||
log, hook = test.NewNullLogger()
|
||||
InitializeLogging(logrus.DebugLevel.String(), dto.FormatterText)
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
s.Fail(err.Error())
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
HTTPLoggingMiddleware(mockHTTPStatusHandler(500)).ServeHTTP(recorder, request)
|
||||
|
||||
assert.Equal(t, 1, len(hook.Entries))
|
||||
assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
|
||||
s.Equal(1, len(hook.Entries))
|
||||
s.Equal(logrus.ErrorLevel, hook.LastEntry().Level)
|
||||
}
|
||||
|
||||
func TestHTTPMiddlewareDebugsWhenStatusOK(t *testing.T) {
|
||||
func (s *MainTestSuite) TestHTTPMiddlewareDebugsWhenStatusOK() {
|
||||
var hook *test.Hook
|
||||
log, hook = test.NewNullLogger()
|
||||
InitializeLogging(logrus.DebugLevel.String(), dto.FormatterText)
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
s.Fail(err.Error())
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
HTTPLoggingMiddleware(mockHTTPStatusHandler(200)).ServeHTTP(recorder, request)
|
||||
|
||||
assert.Equal(t, 1, len(hook.Entries))
|
||||
assert.Equal(t, logrus.DebugLevel, hook.LastEntry().Level)
|
||||
s.Equal(1, len(hook.Entries))
|
||||
s.Equal(logrus.DebugLevel, hook.LastEntry().Level)
|
||||
}
|
||||
|
@ -2,10 +2,7 @@ package nullio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type responseWriterStub struct {
|
||||
@ -19,7 +16,7 @@ func (r *responseWriterStub) Header() http.Header {
|
||||
func (r *responseWriterStub) WriteHeader(_ int) {
|
||||
}
|
||||
|
||||
func TestContentLengthWriter_Write(t *testing.T) {
|
||||
func (s *MainTestSuite) TestContentLengthWriter_Write() {
|
||||
header := http.Header(make(map[string][]string))
|
||||
buf := &responseWriterStub{header: header}
|
||||
writer := &ContentLengthWriter{Target: buf}
|
||||
@ -29,20 +26,20 @@ func TestContentLengthWriter_Write(t *testing.T) {
|
||||
part3 := []byte("AG")
|
||||
|
||||
count, err := writer.Write(part1)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(part1), count)
|
||||
assert.Empty(t, buf.String())
|
||||
assert.Equal(t, "", header.Get("Content-Length"))
|
||||
s.Require().NoError(err)
|
||||
s.Equal(len(part1), count)
|
||||
s.Empty(buf.String())
|
||||
s.Equal("", header.Get("Content-Length"))
|
||||
|
||||
count, err = writer.Write(part2)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(part2), count)
|
||||
assert.Equal(t, "FL", buf.String())
|
||||
assert.Equal(t, contentLength, header.Get("Content-Length"))
|
||||
s.Require().NoError(err)
|
||||
s.Equal(len(part2), count)
|
||||
s.Equal("FL", buf.String())
|
||||
s.Equal(contentLength, header.Get("Content-Length"))
|
||||
|
||||
count, err = writer.Write(part3)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(part3), count)
|
||||
assert.Equal(t, "FLAG", buf.String())
|
||||
assert.Equal(t, contentLength, header.Get("Content-Length"))
|
||||
s.Require().NoError(err)
|
||||
s.Equal(len(part3), count)
|
||||
s.Equal("FLAG", buf.String())
|
||||
s.Equal(contentLength, header.Get("Content-Length"))
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package nullio
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"testing"
|
||||
)
|
||||
@ -12,12 +13,13 @@ func TestLs2JsonTestSuite(t *testing.T) {
|
||||
}
|
||||
|
||||
type Ls2JsonTestSuite struct {
|
||||
suite.Suite
|
||||
tests.MemoryLeakTestSuite
|
||||
buf *bytes.Buffer
|
||||
writer *Ls2JsonWriter
|
||||
}
|
||||
|
||||
func (s *Ls2JsonTestSuite) SetupTest() {
|
||||
s.MemoryLeakTestSuite.SetupTest()
|
||||
s.buf = &bytes.Buffer{}
|
||||
s.writer = &Ls2JsonWriter{Target: s.buf, Ctx: context.Background()}
|
||||
}
|
||||
|
@ -2,23 +2,30 @@ package nullio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const shortTimeout = 100 * time.Millisecond
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestReader_Read(t *testing.T) {
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestReader_Read() {
|
||||
read := func(reader io.Reader, ret chan<- bool) {
|
||||
p := make([]byte, 0, 5)
|
||||
_, err := reader.Read(p)
|
||||
assert.ErrorIs(t, io.EOF, err)
|
||||
s.ErrorIs(io.EOF, err)
|
||||
close(ret)
|
||||
}
|
||||
|
||||
t.Run("WithContext_DoesNotReturnImmediately", func(t *testing.T) {
|
||||
s.Run("WithContext_DoesNotReturnImmediately", func() {
|
||||
readingContext, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
@ -27,27 +34,27 @@ func TestReader_Read(t *testing.T) {
|
||||
|
||||
select {
|
||||
case <-readerReturned:
|
||||
assert.Fail(t, "The reader returned before the timeout was reached")
|
||||
case <-time.After(shortTimeout):
|
||||
s.Fail("The reader returned before the timeout was reached")
|
||||
case <-time.After(tests.ShortTimeout):
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("WithoutContext_DoesReturnImmediately", func(t *testing.T) {
|
||||
s.Run("WithoutContext_DoesReturnImmediately", func() {
|
||||
readerReturned := make(chan bool)
|
||||
go read(&Reader{}, readerReturned)
|
||||
|
||||
select {
|
||||
case <-readerReturned:
|
||||
case <-time.After(shortTimeout):
|
||||
assert.Fail(t, "The reader returned before the timeout was reached")
|
||||
case <-time.After(tests.ShortTimeout):
|
||||
s.Fail("The reader returned before the timeout was reached")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestReadWriterWritesEverything(t *testing.T) {
|
||||
func (s *MainTestSuite) TestReadWriterWritesEverything() {
|
||||
readWriter := &ReadWriter{}
|
||||
p := []byte{1, 2, 3}
|
||||
n, err := readWriter.Write(p)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, len(p), n)
|
||||
s.NoError(err)
|
||||
s.Equal(len(p), n)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"github.com/influxdata/influxdb-client-go/v2/api/write"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"testing"
|
||||
"time"
|
||||
@ -15,12 +14,13 @@ func TestRunnerPoolTestSuite(t *testing.T) {
|
||||
}
|
||||
|
||||
type ObjectPoolTestSuite struct {
|
||||
suite.Suite
|
||||
tests.MemoryLeakTestSuite
|
||||
objectStorage *localStorage[any]
|
||||
object int
|
||||
}
|
||||
|
||||
func (s *ObjectPoolTestSuite) SetupTest() {
|
||||
s.MemoryLeakTestSuite.SetupTest()
|
||||
s.objectStorage = NewLocalStorage[any]()
|
||||
s.object = 42
|
||||
}
|
||||
@ -113,7 +113,15 @@ func (s *ObjectPoolTestSuite) TestLenChangesOnStoreContentChange() {
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewMonitoredLocalStorage_Callback(t *testing.T) {
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestNewMonitoredLocalStorage_Callback() {
|
||||
callbackCalls := 0
|
||||
callbackAdditions := 0
|
||||
callbackDeletions := 0
|
||||
@ -131,40 +139,40 @@ func TestNewMonitoredLocalStorage_Callback(t *testing.T) {
|
||||
beforeAdditions := callbackAdditions
|
||||
beforeDeletions := callbackDeletions
|
||||
test()
|
||||
assert.Equal(t, beforeTotal+totalCalls, callbackCalls)
|
||||
assert.Equal(t, beforeAdditions+additions, callbackAdditions)
|
||||
assert.Equal(t, beforeDeletions+deletions, callbackDeletions)
|
||||
s.Equal(beforeTotal+totalCalls, callbackCalls)
|
||||
s.Equal(beforeAdditions+additions, callbackAdditions)
|
||||
s.Equal(beforeDeletions+deletions, callbackDeletions)
|
||||
}
|
||||
|
||||
t.Run("Add", func(t *testing.T) {
|
||||
s.Run("Add", func() {
|
||||
assertCallbackCounts(func() {
|
||||
os.Add("id 1", "object 1")
|
||||
}, 1, 1, 0)
|
||||
})
|
||||
|
||||
t.Run("Delete", func(t *testing.T) {
|
||||
s.Run("Delete", func() {
|
||||
assertCallbackCounts(func() {
|
||||
os.Delete("id 1")
|
||||
}, 1, 0, 1)
|
||||
})
|
||||
|
||||
t.Run("List", func(t *testing.T) {
|
||||
s.Run("List", func() {
|
||||
assertCallbackCounts(func() {
|
||||
os.List()
|
||||
}, 0, 0, 0)
|
||||
})
|
||||
|
||||
t.Run("Pop", func(t *testing.T) {
|
||||
s.Run("Pop", func() {
|
||||
os.Add("id 1", "object 1")
|
||||
|
||||
assertCallbackCounts(func() {
|
||||
o, ok := os.Pop("id 1")
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "object 1", o)
|
||||
s.True(ok)
|
||||
s.Equal("object 1", o)
|
||||
}, 1, 0, 1)
|
||||
})
|
||||
|
||||
t.Run("Purge", func(t *testing.T) {
|
||||
s.Run("Purge", func() {
|
||||
os.Add("id 1", "object 1")
|
||||
os.Add("id 2", "object 2")
|
||||
|
||||
@ -173,20 +181,17 @@ func TestNewMonitoredLocalStorage_Callback(t *testing.T) {
|
||||
}, 2, 0, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewMonitoredLocalStorage_Periodically(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
func (s *MainTestSuite) TestNewMonitoredLocalStorage_Periodically() {
|
||||
callbackCalls := 0
|
||||
NewMonitoredLocalStorage[string]("testMeasurement", func(p *write.Point, o string, eventType EventType) {
|
||||
callbackCalls++
|
||||
assert.Equal(t, Periodically, eventType)
|
||||
}, 2*tests.ShortTimeout, ctx)
|
||||
s.Equal(Periodically, eventType)
|
||||
}, 2*tests.ShortTimeout, s.TestCtx)
|
||||
|
||||
<-time.After(tests.ShortTimeout)
|
||||
assert.Equal(t, 0, callbackCalls)
|
||||
s.Equal(0, callbackCalls)
|
||||
<-time.After(2 * tests.ShortTimeout)
|
||||
assert.Equal(t, 1, callbackCalls)
|
||||
s.Equal(1, callbackCalls)
|
||||
<-time.After(2 * tests.ShortTimeout)
|
||||
assert.Equal(t, 2, callbackCalls)
|
||||
s.Equal(2, callbackCalls)
|
||||
}
|
||||
|
@ -4,12 +4,20 @@ import (
|
||||
"context"
|
||||
"github.com/openHPI/poseidon/pkg/dto"
|
||||
"github.com/openHPI/poseidon/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMergeContext_Deadline(t *testing.T) {
|
||||
type MainTestSuite struct {
|
||||
tests.MemoryLeakTestSuite
|
||||
}
|
||||
|
||||
func TestMainTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(MainTestSuite))
|
||||
}
|
||||
|
||||
func (s *MainTestSuite) TestMergeContext_Deadline() {
|
||||
ctxWithoutDeadline := context.Background()
|
||||
earlyDeadline := time.Now().Add(time.Second)
|
||||
ctxWithEarlyDeadline, cancel := context.WithDeadline(context.Background(), earlyDeadline)
|
||||
@ -20,11 +28,11 @@ func TestMergeContext_Deadline(t *testing.T) {
|
||||
ctx := NewMergeContext([]context.Context{ctxWithoutDeadline, ctxWithEarlyDeadline, ctxWithLateDeadline})
|
||||
deadline, ok := ctx.Deadline()
|
||||
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, earlyDeadline, deadline, "The ealiest deadline is returned")
|
||||
s.True(ok)
|
||||
s.Equal(earlyDeadline, deadline, "The ealiest deadline is returned")
|
||||
}
|
||||
|
||||
func TestMergeContext_Done(t *testing.T) {
|
||||
func (s *MainTestSuite) TestMergeContext_Done() {
|
||||
ctxWithoutDeadline := context.Background()
|
||||
ctxWithEarlyDeadline, cancel := context.WithTimeout(context.Background(), 2*tests.ShortTimeout)
|
||||
defer cancel()
|
||||
@ -35,7 +43,7 @@ func TestMergeContext_Done(t *testing.T) {
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
assert.Fail(t, "mergeContext is done before any of its parents")
|
||||
s.Fail("mergeContext is done before any of its parents")
|
||||
return
|
||||
case <-time.After(tests.ShortTimeout):
|
||||
}
|
||||
@ -43,27 +51,27 @@ func TestMergeContext_Done(t *testing.T) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(3 * tests.ShortTimeout):
|
||||
assert.Fail(t, "mergeContext is not done after the earliest of its parents")
|
||||
s.Fail("mergeContext is not done after the earliest of its parents")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeContext_Err(t *testing.T) {
|
||||
func (s *MainTestSuite) TestMergeContext_Err() {
|
||||
ctxWithoutDeadline := context.Background()
|
||||
ctxCancelled, cancel := context.WithCancel(context.Background())
|
||||
ctx := NewMergeContext([]context.Context{ctxWithoutDeadline, ctxCancelled})
|
||||
|
||||
assert.NoError(t, ctx.Err())
|
||||
s.NoError(ctx.Err())
|
||||
cancel()
|
||||
assert.Error(t, ctx.Err())
|
||||
s.Error(ctx.Err())
|
||||
}
|
||||
|
||||
func TestMergeContext_Value(t *testing.T) {
|
||||
func (s *MainTestSuite) TestMergeContext_Value() {
|
||||
ctxWithAValue := context.WithValue(context.Background(), dto.ContextKey("keyA"), "valueA")
|
||||
ctxWithAnotherValue := context.WithValue(context.Background(), dto.ContextKey("keyB"), "valueB")
|
||||
ctx := NewMergeContext([]context.Context{ctxWithAValue, ctxWithAnotherValue})
|
||||
|
||||
assert.Equal(t, "valueA", ctx.Value(dto.ContextKey("keyA")))
|
||||
assert.Equal(t, "valueB", ctx.Value(dto.ContextKey("keyB")))
|
||||
assert.Nil(t, ctx.Value("keyC"))
|
||||
s.Equal("valueA", ctx.Value(dto.ContextKey("keyA")))
|
||||
s.Equal("valueB", ctx.Value(dto.ContextKey("keyB")))
|
||||
s.Nil(ctx.Value("keyC"))
|
||||
}
|
||||
|
Reference in New Issue
Block a user