Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
manimovassagh committed Oct 18, 2024
1 parent d2840c7 commit a5f83ab
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 5 deletions.
24 changes: 19 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:

jobs:
build:

runs-on: ubuntu-latest

steps:
Expand All @@ -24,7 +23,6 @@ jobs:
with:
go-version: '1.23.0'


# Install dependencies
- name: Install dependencies
run: go mod download
Expand All @@ -33,6 +31,22 @@ jobs:
- name: Build the project
run: go build ./...

# Run tests
- name: Run tests
run: go test ./... -v
# Run tests and generate JUnit report
- name: Run tests and generate JUnit report
run: |
mkdir -p test-reports
go test ./... -v 2>&1 | tee >(go-junit-report > test-reports/junit.xml)
# Upload the test report as an artifact
- name: Upload test report
uses: actions/upload-artifact@v3
with:
name: junit-test-report
path: test-reports/junit.xml

# Parse JUnit results and create a test summary
- name: Create test summary
uses: dorny/test-summary@v2
with:
format: markdown
test-files: test-reports/junit.xml
7 changes: 7 additions & 0 deletions internal/commands/appendonly.aof
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ $3
key
$5
value
*3
$3
SET
$3
key
$5
value
103 changes: 103 additions & 0 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package server

import (
"net"
"testing"
"time"
)

// MockListener is a mock implementation of net.Listener
type MockListener struct {
connChan chan net.Conn
closed bool
}

func NewMockListener() *MockListener {
return &MockListener{
connChan: make(chan net.Conn),
}
}

func (ml *MockListener) Accept() (net.Conn, error) {
if ml.closed {
return nil, net.ErrClosed
}
conn, ok := <-ml.connChan
if !ok {
return nil, net.ErrClosed
}
return conn, nil
}

func (ml *MockListener) Close() error {
if !ml.closed {
ml.closed = true
close(ml.connChan)
}
return nil
}

func (ml *MockListener) Addr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8080,
}
}

// MockConn is a mock implementation of net.Conn for testing
type MockConn struct{}

func (mc *MockConn) Read(b []byte) (n int, err error) { return 0, nil }
func (mc *MockConn) Write(b []byte) (n int, err error) { return len(b), nil }
func (mc *MockConn) Close() error { return nil }
func (mc *MockConn) LocalAddr() net.Addr { return nil }
func (mc *MockConn) RemoteAddr() net.Addr { return nil }
func (mc *MockConn) SetDeadline(t time.Time) error { return nil }
func (mc *MockConn) SetReadDeadline(t time.Time) error { return nil }
func (mc *MockConn) SetWriteDeadline(t time.Time) error { return nil }

// TestServerRun tests the server's ability to accept connections and handle them
func TestServerRun(t *testing.T) {
// Create a mock listener and server
mockListener := NewMockListener()
server := New("127.0.0.1:8080")

// Mock the listener to return our mock listener instead of a real one
go func() {
// Simulate a client connection being accepted
mockListener.connChan <- &MockConn{}
}()

// Run the server in a separate goroutine and close it after a short delay
go func() {
_ = server.Run()
}()

// Sleep briefly to give the server time to run
time.Sleep(100 * time.Millisecond)

// Check if the server accepted a connection and handled it
select {
case conn := <-mockListener.connChan:
if conn == nil {
t.Fatal("Expected a connection, but got nil")
}
default:
t.Fatal("Server did not accept any connections")
}

// Close the mock listener to stop the server
mockListener.Close()
}

// TestServerAcceptFailure tests if the server properly handles connection acceptance failure
func TestServerAcceptFailure(t *testing.T) {
// Create a server
server := New("invalid_address")

// Run the server and expect it to return an error
err := server.Run()
if err == nil {
t.Fatal("Expected an error when trying to run the server with an invalid address")
}
}

0 comments on commit a5f83ab

Please sign in to comment.