Skip to content

Commit

Permalink
chore: add test for nsqlookupd
Browse files Browse the repository at this point in the history
  • Loading branch information
shivamsouravjha committed Feb 7, 2025
1 parent 29304eb commit e9758fd
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 9 deletions.
16 changes: 7 additions & 9 deletions nsqd/statsd_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package nsqd

import (
"testing"
"testing"
)



// Test generated using Keploy
func TestPercentile_EmptyArray(t *testing.T) {
data := []uint64{}
result := percentile(50, data, len(data))
expected := uint64(0)
if result != expected {
t.Errorf("Expected %v, got %v", expected, result)
}
data := []uint64{}
result := percentile(50, data, len(data))
expected := uint64(0)
if result != expected {
t.Errorf("Expected %v, got %v", expected, result)
}
}
41 changes: 41 additions & 0 deletions nsqlookupd/lookup_protocol_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,47 @@ func TestIOLoopReturnsClientErrWhenSendSucceeds(t *testing.T) {
testIOLoopReturnsClientErr(t, fakeConn)
}

// Test generated using Keploy
func TestIOLoopSendResponseError(t *testing.T) {
fakeConn := test.NewFakeNetConn()

// Simulate a valid command that will cause a response
fakeConn.ReadFunc = func(b []byte) (int, error) {
return copy(b, []byte("PING\n")), nil
}

// Simulate an error on write to cause SendResponse to fail
firstWrite := true
fakeConn.WriteFunc = func(b []byte) (int, error) {
if firstWrite {
firstWrite = false
return len(b), nil
}
return 0, errors.New("write error")
}

opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
nsqlookupd, err := New(opts)
test.Nil(t, err)
prot := &LookupProtocolV1{nsqlookupd: nsqlookupd}
client := prot.NewClient(fakeConn)

errChan := make(chan error)
go func() {
errChan <- prot.IOLoop(client)
}()

// Wait for IOLoop to exit
var ioLoopErr error
select {
case ioLoopErr = <-errChan:
case <-time.After(time.Second):
t.Fatal("IOLoop didn't exit")
}
test.NotNil(t, ioLoopErr)
}

func testIOLoopReturnsClientErr(t *testing.T, fakeConn test.FakeNetConn) {
fakeConn.ReadFunc = func(b []byte) (int, error) {
return copy(b, []byte("INVALID_COMMAND\n")), nil
Expand Down
43 changes: 43 additions & 0 deletions nsqlookupd/nsqlookupd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,46 @@ func TestTombstonedNodes(t *testing.T) {
test.Equal(t, topicName, producers[0].Topics[0].Topic)
test.Equal(t, true, producers[0].Topics[0].Tombstoned)
}

// Test generated using Keploy
func TestNew_NilLogger(t *testing.T) {
opts := NewOptions()
opts.Logger = nil
opts.LogPrefix = "[TEST] "
opts.TCPAddress = "127.0.0.1:0"
opts.HTTPAddress = "127.0.0.1:0"

nsqlookupd, err := New(opts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer nsqlookupd.Exit()

if nsqlookupd.opts.Logger == nil {
t.Fatalf("expected logger to be initialized, but it was nil")
}
}

// Test generated using Keploy
func TestNew_TCPListenerError(t *testing.T) {
opts := NewOptions()
opts.TCPAddress = "invalid_address"
opts.HTTPAddress = "127.0.0.1:0"

_, err := New(opts)
if err == nil {
t.Fatalf("expected error but got nil")
}
}

// Test generated using Keploy
func TestNew_HTTPListenerError(t *testing.T) {
opts := NewOptions()
opts.TCPAddress = "127.0.0.1:0"
opts.HTTPAddress = "invalid_address"

_, err := New(opts)
if err == nil {
t.Fatalf("expected error but got nil")
}
}
33 changes: 33 additions & 0 deletions nsqlookupd/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nsqlookupd

import (
"testing"
"time"

"github.com/nsqio/nsq/internal/lg"
)

// Test generated using Keploy
func TestNewOptions_DefaultValues_ValidHostname(t *testing.T) {

opts := NewOptions()

if opts.LogPrefix != "[nsqlookupd] " {
t.Errorf("Expected LogPrefix to be '[nsqlookupd] ', got %v", opts.LogPrefix)
}
if opts.LogLevel != lg.INFO {
t.Errorf("Expected LogLevel to be lg.INFO, got %v", opts.LogLevel)
}
if opts.TCPAddress != "0.0.0.0:4160" {
t.Errorf("Expected TCPAddress to be '0.0.0.0:4160', got %v", opts.TCPAddress)
}
if opts.HTTPAddress != "0.0.0.0:4161" {
t.Errorf("Expected HTTPAddress to be '0.0.0.0:4161', got %v", opts.HTTPAddress)
}
if opts.InactiveProducerTimeout != 300*time.Second {
t.Errorf("Expected InactiveProducerTimeout to be 300 seconds, got %v", opts.InactiveProducerTimeout)
}
if opts.TombstoneLifetime != 45*time.Second {
t.Errorf("Expected TombstoneLifetime to be 45 seconds, got %v", opts.TombstoneLifetime)
}
}

0 comments on commit e9758fd

Please sign in to comment.