Skip to content

Commit

Permalink
refactor: update gRPC client initialization in model service tests
Browse files Browse the repository at this point in the history
- Replaced direct gRPC client instantiation with a call to GetGrpcClient().Start() for improved consistency and maintainability across test cases.
- Cleaned up unnecessary imports in model_service.go to streamline the codebase.
- Enhanced test setup by ensuring the gRPC client is properly initialized before executing service methods.
  • Loading branch information
tikazyq committed Dec 21, 2024
1 parent 41e973a commit 75da4ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
5 changes: 2 additions & 3 deletions core/models/client/model_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package client

import (
"encoding/json"
"reflect"
"sync"

"github.com/crawlab-team/crawlab/core/grpc/client"
"github.com/crawlab-team/crawlab/core/interfaces"
nodeconfig "github.com/crawlab-team/crawlab/core/node/config"
"github.com/crawlab-team/crawlab/db/mongo"
"github.com/crawlab-team/crawlab/grpc"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"reflect"
"sync"
)

var (
Expand Down
45 changes: 15 additions & 30 deletions core/models/client/model_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client_test

import (
"context"
client2 "github.com/crawlab-team/crawlab/core/grpc/client"
"github.com/crawlab-team/crawlab/core/models/models"
"testing"
"time"
Expand All @@ -15,8 +16,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func setupTestDB() {
Expand Down Expand Up @@ -58,9 +57,8 @@ func TestModelService_GetById(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
res, err := clientSvc.GetById(m.Id)
Expand All @@ -85,9 +83,8 @@ func TestModelService_GetOne(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
res, err := clientSvc.GetOne(bson.M{"name": m.Name}, nil)
Expand All @@ -112,9 +109,8 @@ func TestModelService_GetMany(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
res, err := clientSvc.GetMany(bson.M{"name": m.Name}, nil)
Expand All @@ -140,9 +136,8 @@ func TestModelService_DeleteById(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
err = clientSvc.DeleteById(m.Id)
Expand All @@ -169,9 +164,8 @@ func TestModelService_DeleteOne(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
err = clientSvc.DeleteOne(bson.M{"name": m.Name})
Expand All @@ -198,9 +192,8 @@ func TestModelService_DeleteMany(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
err = clientSvc.DeleteMany(bson.M{"name": m.Name})
Expand All @@ -227,9 +220,8 @@ func TestModelService_UpdateById(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
err = clientSvc.UpdateById(m.Id, bson.M{"$set": bson.M{"name": "New Name"}})
Expand All @@ -256,9 +248,8 @@ func TestModelService_UpdateOne(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
err = clientSvc.UpdateOne(bson.M{"name": m.Name}, bson.M{"$set": bson.M{"name": "New Name"}})
Expand Down Expand Up @@ -289,9 +280,8 @@ func TestModelService_UpdateMany(t *testing.T) {
require.Nil(t, err)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
err = clientSvc.UpdateMany(bson.M{"name": "Test Name"}, bson.M{"$set": bson.M{"name": "New Name"}})
Expand All @@ -318,9 +308,8 @@ func TestModelService_ReplaceById(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
m.Name = "New Name"
Expand Down Expand Up @@ -348,9 +337,8 @@ func TestModelService_ReplaceOne(t *testing.T) {
m.SetId(id)
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err = client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
m.Name = "New Name"
Expand All @@ -369,9 +357,8 @@ func TestModelService_InsertOne(t *testing.T) {
go startSvr(svr)
defer stopSvr(svr)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err := client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
m := models.TestModel{
Expand All @@ -392,9 +379,8 @@ func TestModelService_InsertMany(t *testing.T) {
go startSvr(svr)
defer stopSvr(svr)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err := client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
testModels := []models.TestModel{
Expand Down Expand Up @@ -427,9 +413,8 @@ func TestModelService_Count(t *testing.T) {
}
time.Sleep(100 * time.Millisecond)

c, err := grpc.NewClient("localhost:9666", grpc.WithTransportCredentials(insecure.NewCredentials()))
err := client2.GetGrpcClient().Start()
require.Nil(t, err)
c.Connect()

clientSvc := client.NewModelService[models.TestModel]()
count, err := clientSvc.Count(bson.M{})
Expand Down

0 comments on commit 75da4ff

Please sign in to comment.