Skip to content

Commit

Permalink
Rachit77/tests (#137)
Browse files Browse the repository at this point in the history
* wip

* wip

* change permission

* wip

* wip

* wip

* lint fix
  • Loading branch information
rachit77 authored Sep 24, 2024
1 parent 80fd1fa commit 766c3e3
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 1 deletion.
18 changes: 17 additions & 1 deletion datastreamer/datastreamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,20 @@ func TestServer(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(3), entryNumber)

// Case: Start atomic operation with atomic operation in progress -> FAIL
_ = streamServer.StartAtomicOp()
err = streamServer.StartAtomicOp()
_ = streamServer.CommitAtomicOp()
require.EqualError(t, datastreamer.ErrStartAtomicOpNotAllowed, err.Error())

// Case: Commit atomic operation without starting atomic operation -> FAIL
err = streamServer.CommitAtomicOp()
require.NoError(t, err)
require.EqualError(t, datastreamer.ErrCommitNotAllowed, err.Error())

// Case: AddStreamBookmark without atomic operation in progress -> FAIL
entryNumber, err = streamServer.AddStreamBookmark(testBookmark.Encode())
require.Equal(t, uint64(0), entryNumber)
require.EqualError(t, datastreamer.ErrAddEntryNotAllowed, err.Error())

// Check get data between 2 bookmarks
data, err := streamServer.GetDataBetweenBookmarks(testBookmark.Encode(), testBookmark2.Encode())
Expand Down Expand Up @@ -356,6 +368,10 @@ func TestServer(t *testing.T) {
err = streamServer.RollbackAtomicOp()
require.NoError(t, err)

// Case: Rollback operation without starting atomic operation -> FAIL
err = streamServer.RollbackAtomicOp()
require.EqualError(t, datastreamer.ErrRollbackNotAllowed, err.Error())

// Case: Get entry data of previous rollback entry number (doesn't exist) -> FAIL
entry, err = streamServer.GetEntry(7)
require.EqualError(t, datastreamer.ErrInvalidEntryNumber, err.Error())
Expand Down
72 changes: 72 additions & 0 deletions datastreamer/streambookmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package datastreamer

import (
"os"
"testing"

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

func createTempDB(t *testing.T) *StreamBookmark {
t.Helper()

tempFile := "test_bookmarks_db"
b, err := NewBookmark(tempFile)
if err != nil {
t.Fatalf("Failed to create bookmark: %v", err)
}

return b
}

func cleanUpDB(t *testing.T, b *StreamBookmark) {
t.Helper()

err := b.db.Close()
if err != nil {
t.Fatalf("Failed to close bookmark database: %v", err)
}

err = os.RemoveAll(b.dbName)
if err != nil {
t.Fatalf("Failed to remove test database: %v", err)
}
}

func TestNewBookmark(t *testing.T) {
b := createTempDB(t)
defer cleanUpDB(t, b)

if b == nil || b.db == nil {
t.Fatalf("Expected non-nil bookmark and database")
}
}

func TestAddBookmark(t *testing.T) {
b := createTempDB(t)
defer cleanUpDB(t, b)

bookmark := []byte("testBookmark")
entryNum := uint64(12345)

err := b.AddBookmark(bookmark, entryNum)
if err != nil {
t.Fatalf("Failed to add bookmark: %v", err)
}

value, err := b.GetBookmark(bookmark)
if err != nil {
t.Fatalf("Failed to get bookmark after adding: %v", err)
}

assert.Equal(t, entryNum, value, "Expected bookmark value %d, got %d", entryNum, value)
}

func TestGetBookmarkNotFound(t *testing.T) {
b := createTempDB(t)
defer cleanUpDB(t, b)

nonExistentBookmark := []byte("nonExistentBookmark")
_, err := b.GetBookmark(nonExistentBookmark)
assert.Error(t, err, "Expected error when getting a non-existent bookmark")
}
56 changes: 56 additions & 0 deletions datastreamer/streamfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package datastreamer

import (
"os"
"testing"

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

func setupTestFile(t *testing.T, filename string) *StreamFile {
t.Helper()

sf, err := NewStreamFile(filename, 1, 12345, 1)
assert.NoError(t, err)
assert.NotNil(t, sf)

return sf
}

func cleanupTestFile(filename string) {
_ = os.Remove(filename)
}

func TestNewStreamFile(t *testing.T) {
filename := "test_streamfile.bin"
defer cleanupTestFile(filename)

sf := setupTestFile(t, filename)

assert.Equal(t, filename, sf.fileName)
assert.Equal(t, uint32(PageDataSize), sf.pageSize)
assert.Equal(t, uint64(4096), sf.header.TotalLength)
assert.Equal(t, uint64(0), sf.header.TotalEntries)

info, err := os.Stat(filename)
assert.NoError(t, err)
assert.Equal(t, int64(PageHeaderSize+initPages*PageDataSize), info.Size())
}

func TestWriteAndReadHeader(t *testing.T) {
filename := "test_streamfile_header.bin"
defer cleanupTestFile(filename)

sf := setupTestFile(t, filename)

sf.header.TotalEntries = 10
sf.header.TotalLength = 4096
err := sf.writeHeaderEntry()
assert.NoError(t, err)

err = sf.readHeaderEntry()
assert.NoError(t, err)

assert.Equal(t, uint64(10), sf.header.TotalEntries)
assert.Equal(t, uint64(4096), sf.header.TotalLength)
}
40 changes: 40 additions & 0 deletions datastreamer/streamserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package datastreamer

import (
"testing"

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

func TestProcessCommand(t *testing.T) {
server := new(StreamServer)
cli := &client{status: csSyncing}

// Test CmdStart
err := server.processCommand(CmdStart, cli)
assert.EqualError(t, ErrClientAlreadyStarted, err.Error())

// Test CmdStartBookmark
err = server.processCommand(CmdStartBookmark, cli)
assert.EqualError(t, ErrClientAlreadyStarted, err.Error())

// Test CmdStop
err = server.processCommand(CmdStop, cli)
assert.EqualError(t, ErrClientAlreadyStopped, err.Error())

// Test CmdHeader
err = server.processCommand(CmdHeader, cli)
assert.EqualError(t, ErrHeaderCommandNotAllowed, err.Error())

// Test CmdEntry
err = server.processCommand(CmdEntry, cli)
assert.EqualError(t, ErrEntryCommandNotAllowed, err.Error())

// Test CmdBookmark
err = server.processCommand(CmdBookmark, cli)
assert.EqualError(t, ErrBookmarkCommandNotAllowed, err.Error())

// Test invalid command
err = server.processCommand(Command(100), cli)
assert.EqualError(t, ErrInvalidCommand, err.Error())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.uber.org/multierr v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1Fof
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
Expand Down
89 changes: 89 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package log

import (
"errors"
"fmt"
"testing"

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

func TestLogNotInitialized(t *testing.T) {
Info("Test log.Info value is ", 10)
Infof("Test log.Infof %d", 10)
Infow("Test log.Infow", "value", 10)
Debug("Test log.Debug value is ", 10)
Debugf("Test log.Debugf %d", 10)
Debugw("Test log.Debugw value", 10)
Error("Test log.Error value is ", 10)
Errorf("Test log.Errorf %d", 10)
Errorw("Test log.Errorw value", 10)
Warn("Test log.Warn value is ", 10)
Warnf("Test log.Warnf %d", 10)
Warnw("Test log.Warnw value", 10)
}
Expand All @@ -35,3 +43,84 @@ func TestLog(t *testing.T) {
Warnf("Test log.Warnf %d", 10)
Warnw("Test log.Warnw value", 10)
}

func TestLogger_WithFields(t *testing.T) {
cfg := Config{
Environment: EnvironmentDevelopment,
Level: "debug",
Outputs: []string{"stderr"},
}
Init(cfg)

originalLogger := WithFields("originalField", "originalValue")
derivedLogger := originalLogger.WithFields("newField", "newValue")

originalCore := originalLogger.x.Desugar().Core()
assert.NotNil(t, originalCore)
assert.NotEqual(t, derivedLogger.x, originalLogger.x)

derivedCore := derivedLogger.x.Desugar().Core()
assert.NotNil(t, derivedCore)
assert.NotEqual(t, derivedCore, originalCore)
}

func TestSprintStackTrace(t *testing.T) {
err := func() error {
return tracerr.Wrap(func() error {
return tracerr.New("dummy error")
}())
}()

st := tracerr.StackTrace(err)
fmt.Println(st)

stackTraceStr := sprintStackTrace(st)
fmt.Println(stackTraceStr)

assert.Contains(t, stackTraceStr, "/log/log_test.go")
assert.Contains(t, stackTraceStr, "TestSprintStackTrace")
}

func TestAppendStackTraceMaybeArgs(t *testing.T) {
err := errors.New("test error")
args := []interface{}{"some value", err}
newArgs := appendStackTraceMaybeArgs(args)

assert.Greater(t, len(newArgs), len(args))

stackTraceStr, ok := newArgs[len(newArgs)-1].(string)
fmt.Println(stackTraceStr)
assert.True(t, ok)
assert.Contains(t, stackTraceStr, "/log/log_test.go")
assert.Contains(t, stackTraceStr, "TestAppendStackTraceMaybeArgs")
}

func TestAppendStackTraceMaybeKV(t *testing.T) {
msg := "Test message"

// Test case: No error in key-value pairs
kv := []interface{}{"key1", "value1", "key2", "value2"}
result := appendStackTraceMaybeKV(msg, kv)
assert.Equal(t, msg, result, "Expected message to be unchanged when no error is present")

// Test case: Error in key-value pairs
err := errors.New("Test error")
wrappedErr := tracerr.Wrap(err)
expectedErrMsg := fmt.Sprintf("%v: %v", msg, wrappedErr.Error())
kv = []interface{}{"key1", "value1", "errorKey", err}
result = appendStackTraceMaybeKV(msg, kv)

assert.Contains(t, result, expectedErrMsg, "Expected message to include the error and its stack trace")
assert.Contains(t, result, "log_test.go", "Expected stack trace to include 'log_test.go'")
assert.Contains(
t,
result,
"TestAppendStackTraceMaybeKV",
"Expected stack trace to include 'TestAppendStackTraceMaybeKV'",
)

// Test case: Error at an even index should be ignored
kv = []interface{}{err, "value2", "key2", "value2"}
result = appendStackTraceMaybeKV(msg, kv)
assert.Equal(t, msg, result, "Expected message to be unchanged when error is at an odd index")
}
Loading

0 comments on commit 766c3e3

Please sign in to comment.