-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Unit tests code coverage improvement (#1781)
Signed-off-by: Samhith Kakarla <[email protected]>
- Loading branch information
1 parent
b6ab99f
commit 41e7878
Showing
25 changed files
with
1,684 additions
and
55 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package nats | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewClientPool_Success(t *testing.T) { | ||
os.Setenv(dfv1.EnvISBSvcJetStreamURL, "nats://localhost:4222") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamUser, "user") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamPassword, "password") | ||
ctx := context.Background() | ||
pool, err := NewClientPool(ctx) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, pool) | ||
assert.Equal(t, 3, pool.clients.Len()) // Check if the pool size matches the default clientPoolSize | ||
} | ||
|
||
func TestClientPool_NextAvailableClient(t *testing.T) { | ||
os.Setenv(dfv1.EnvISBSvcJetStreamURL, "nats://localhost:4222") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamUser, "user") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamPassword, "password") | ||
ctx := context.Background() | ||
pool, err := NewClientPool(ctx) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, pool) | ||
|
||
client1 := pool.NextAvailableClient() | ||
assert.NotNil(t, client1) | ||
|
||
client2 := pool.NextAvailableClient() | ||
assert.NotNil(t, client2) | ||
|
||
client3 := pool.NextAvailableClient() | ||
assert.NotNil(t, client3) | ||
} | ||
|
||
func TestClientPool_CloseAll(t *testing.T) { | ||
os.Setenv(dfv1.EnvISBSvcJetStreamURL, "nats://localhost:4222") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamUser, "user") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamPassword, "password") | ||
ctx := context.Background() | ||
pool, err := NewClientPool(ctx) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, pool) | ||
|
||
for e := pool.clients.Front(); e != nil; e = e.Next() { | ||
client := e.Value.(*Client) | ||
assert.False(t, client.nc.IsClosed()) | ||
} | ||
|
||
pool.CloseAll() | ||
for e := pool.clients.Front(); e != nil; e = e.Next() { | ||
client := e.Value.(*Client) | ||
assert.True(t, client.nc.IsClosed()) | ||
} | ||
} |
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,127 @@ | ||
package nats | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/nats-io/nats.go" | ||
dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" | ||
"github.com/numaproj/numaflow/pkg/shared/logging" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
|
||
natstest "github.com/numaproj/numaflow/pkg/shared/clients/nats/test" | ||
) | ||
|
||
func TestNewNATSClient(t *testing.T) { | ||
// Setting up environment variables for the test | ||
os.Setenv(dfv1.EnvISBSvcJetStreamURL, "nats://localhost:4222") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamUser, "user") | ||
os.Setenv(dfv1.EnvISBSvcJetStreamPassword, "password") | ||
defer os.Clearenv() | ||
|
||
log := zap.NewNop().Sugar() | ||
|
||
ctx := logging.WithLogger(context.Background(), log) | ||
|
||
client, err := NewNATSClient(ctx) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, client) | ||
|
||
// Cleanup | ||
client.Close() | ||
} | ||
|
||
func TestNewNATSClient_Failure(t *testing.T) { | ||
// Simulating environment variable absence | ||
os.Clearenv() | ||
|
||
log := zap.NewNop().Sugar() | ||
ctx := logging.WithLogger(context.Background(), log) | ||
|
||
client, err := NewNATSClient(ctx) | ||
assert.Error(t, err) | ||
assert.Nil(t, client) | ||
} | ||
|
||
func TestSubscribe(t *testing.T) { | ||
s := natstest.RunJetStreamServer(t) | ||
defer s.Shutdown() | ||
|
||
client := NewTestClient(t, s.ClientURL()) | ||
defer client.Close() | ||
|
||
// Create a stream | ||
js, err := client.nc.JetStream() | ||
assert.NoError(t, err) | ||
_, err = js.AddStream(&nats.StreamConfig{ | ||
Name: "TEST_STREAM", | ||
Subjects: []string{"test.subject"}, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
// Subscribe to a subject | ||
sub, err := client.Subscribe("test.subject", "TEST_STREAM") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, sub) | ||
|
||
// Test failure case: Invalid stream | ||
_, err = client.Subscribe("balh", "INVALID_STREAM") | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestBindKVStore(t *testing.T) { | ||
s := natstest.RunJetStreamServer(t) | ||
defer s.Shutdown() | ||
|
||
client := NewTestClient(t, s.ClientURL()) | ||
defer client.Close() | ||
|
||
// Create a KeyValue store | ||
js, err := client.nc.JetStream() | ||
assert.NoError(t, err) | ||
_, err = js.CreateKeyValue(&nats.KeyValueConfig{ | ||
Bucket: "KV_TEST", | ||
}) | ||
assert.NoError(t, err) | ||
|
||
// Bind to the KeyValue store | ||
kvStore, err := client.BindKVStore("KV_TEST") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, kvStore) | ||
|
||
// Test failure case: Invalid KeyValue store | ||
_, err = client.BindKVStore("INVALID_KV") | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestJetStreamContext(t *testing.T) { | ||
s := natstest.RunJetStreamServer(t) | ||
defer s.Shutdown() | ||
|
||
client := NewTestClient(t, s.ClientURL()) | ||
defer client.Close() | ||
|
||
jsCtx, err := client.JetStreamContext() | ||
assert.NoError(t, err) | ||
assert.NotNil(t, jsCtx) | ||
} | ||
|
||
func TestNewTestClient(t *testing.T) { | ||
s := natstest.RunJetStreamServer(t) | ||
defer s.Shutdown() | ||
|
||
client := NewTestClient(t, s.ClientURL()) | ||
assert.NotNil(t, client) | ||
defer client.Close() | ||
} | ||
|
||
func TestClose(t *testing.T) { | ||
s := natstest.RunJetStreamServer(t) | ||
defer s.Shutdown() | ||
|
||
client := NewTestClient(t, s.ClientURL()) | ||
assert.NotNil(t, client) | ||
client.Close() | ||
} |
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,33 @@ | ||
package nats | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDefaultOptions(t *testing.T) { | ||
opts := defaultOptions() | ||
assert.NotNil(t, opts) | ||
assert.Equal(t, 3, opts.clientPoolSize, "default client pool size should be 3") | ||
} | ||
|
||
func TestWithClientPoolSize(t *testing.T) { | ||
opts := defaultOptions() | ||
assert.Equal(t, 3, opts.clientPoolSize, "default client pool size should be 3") | ||
|
||
option := WithClientPoolSize(10) | ||
option(opts) | ||
|
||
assert.Equal(t, 10, opts.clientPoolSize, "client pool size should be set to 10") | ||
} | ||
|
||
func TestCombinedOptions(t *testing.T) { | ||
opts := defaultOptions() | ||
assert.Equal(t, 3, opts.clientPoolSize, "default client pool size should be 3") | ||
|
||
option1 := WithClientPoolSize(5) | ||
option1(opts) | ||
|
||
assert.Equal(t, 5, opts.clientPoolSize, "client pool size should be set to 5") | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.