-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * wip * change permission * wip * wip * wip * lint fix
- Loading branch information
Showing
8 changed files
with
347 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.