From bbcbb1b42ef0e9930c18ceff875485db982e1643 Mon Sep 17 00:00:00 2001 From: Yan kabuki Date: Fri, 30 Dec 2022 14:29:55 -0300 Subject: [PATCH] removing janitor process from cache and clean cache before inserting new entry --- .github/workflows/test.yaml | 6 +++--- internal/adapters/events/forwarder_client.go | 3 ++- .../adapters/events/forwarder_client_test.go | 18 +----------------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index db2c36142..89c8dae21 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -3,7 +3,7 @@ on: push jobs: unit: name: Unit tests - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - name: Set up Go uses: actions/setup-go@v1 @@ -23,7 +23,7 @@ jobs: run: bash <(curl -s https://codecov.io/bash) integration: name: Integration tests - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - name: Set up Go uses: actions/setup-go@v1 @@ -44,7 +44,7 @@ jobs: runtime-integration: name: Runtime Integration tests - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 strategy: fail-fast: false matrix: diff --git a/internal/adapters/events/forwarder_client.go b/internal/adapters/events/forwarder_client.go index b6a121a24..7ed1a53c3 100644 --- a/internal/adapters/events/forwarder_client.go +++ b/internal/adapters/events/forwarder_client.go @@ -54,7 +54,7 @@ type ForwarderClient struct { // NewForwarderClient instantiate a new grpc forwarder client. func NewForwarderClient() *ForwarderClient { - cache := cache.New(24*time.Hour, 25*time.Hour) + cache := cache.New(24*time.Hour, 0) cache.OnEvicted(func(_key string, clientFromCache interface{}) { ForwarderClient := clientFromCache.(*grpc.ClientConn) ForwarderClient.Close() @@ -150,6 +150,7 @@ func (f *ForwarderClient) getGrpcClient(address Address) (pb.GRPCForwarderClient if err != nil { return nil, err } + f.c.DeleteExpired() f.c.Set(string(address), client, cache.DefaultExpiration) return pb.NewGRPCForwarderClient(client), nil } diff --git a/internal/adapters/events/forwarder_client_test.go b/internal/adapters/events/forwarder_client_test.go index daaa18526..6003d7960 100644 --- a/internal/adapters/events/forwarder_client_test.go +++ b/internal/adapters/events/forwarder_client_test.go @@ -33,7 +33,6 @@ import ( "testing" - "github.com/patrickmn/go-cache" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/topfreegames/maestro/internal/core/entities/forwarder" @@ -131,9 +130,6 @@ func TestSendRoomReSync(t *testing.T) { } func TestForwarderClient_SendRoomStatus(t *testing.T) { - type fields struct { - c *cache.Cache - } type args struct { ctx context.Context forwarder forwarder.Forwarder @@ -142,7 +138,6 @@ func TestForwarderClient_SendRoomStatus(t *testing.T) { tests := []struct { name string requestStubFile string - fields fields args args want *pb.Response wantErr assert.ErrorAssertionFunc @@ -150,9 +145,6 @@ func TestForwarderClient_SendRoomStatus(t *testing.T) { { name: "return response with code 200 when request succeeds", requestStubFile: "../../../test/data/events_mock/events-forwarder-grpc-send-room-status-success.json", - fields: fields{ - c: cache.New(time.Minute, time.Minute), - }, args: args{ ctx: context.Background(), forwarder: newForwarder(grpcMockAddress), @@ -167,9 +159,6 @@ func TestForwarderClient_SendRoomStatus(t *testing.T) { { name: "return response with code 500 when request fails", requestStubFile: "../../../test/data/events_mock/events-forwarder-grpc-send-room-status-failure.json", - fields: fields{ - c: cache.New(time.Minute, time.Minute), - }, args: args{ ctx: context.Background(), forwarder: newForwarder(grpcMockAddress), @@ -184,9 +173,6 @@ func TestForwarderClient_SendRoomStatus(t *testing.T) { { name: "return error when connection establishment fails", requestStubFile: "", - fields: fields{ - c: cache.New(time.Minute, time.Minute), - }, args: args{ ctx: context.Background(), forwarder: newForwarder(""), @@ -204,9 +190,7 @@ func TestForwarderClient_SendRoomStatus(t *testing.T) { ) require.NoError(t, err) } - f := &ForwarderClient{ - c: tt.fields.c, - } + f := NewForwarderClient() got, err := f.SendRoomStatus(tt.args.ctx, tt.args.forwarder, &tt.args.in) if !tt.wantErr(t, err, fmt.Sprintf("SendRoomStatus(%v, %v, %v)", tt.args.ctx, tt.args.forwarder, tt.args.in)) { return