Skip to content

Commit

Permalink
fix: only err for any errors returned; fix: use error constant; fix: …
Browse files Browse the repository at this point in the history
…rename git action
  • Loading branch information
poppoerika committed Jan 28, 2022
1 parent f697f89 commit d5bfcc2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Client SDK Go Test
on:
pull_request:
branches: [main]
Expand Down
93 changes: 48 additions & 45 deletions internal/scsmanagers/scs_data_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,57 +44,60 @@ func (dc *ScsDataClient) Close() error {
}

func (dc *ScsDataClient) Set(csr requests.CacheSetRequest) (*responses.SetCacheResponse, error) {
if utility.IsCacheNameValid(csr.CacheName) {
byteKey, errAsBytesKey := asBytes(csr.Key, "Unsupported type for key: ")
if errAsBytesKey != nil {
return nil, errAsBytesKey
}
byteValue, errAsBytesValue := asBytes(csr.Value, "Unsupported type for value: ")
if errAsBytesValue != nil {
return nil, errAsBytesValue
}
var itemTtlMils uint32
if csr.TtlSeconds == 0 {
itemTtlMils = dc.defaultTtlSeconds * 1000
} else {
itemTtlMils = csr.TtlSeconds * 1000
if !utility.IsCacheNameValid(csr.CacheName) {
return nil, scserrors.InvalidInputError("cache name cannot be empty")
}
var defaultTtlMils = dc.defaultTtlSeconds * 1000
var ttlMils = csr.TtlSeconds * 1000
byteKey, err := asBytes(csr.Key, "Unsupported type for key: ")
if err != nil {
return nil, err
}
byteValue, err := asBytes(csr.Value, "Unsupported type for value: ")
if err != nil {
return nil, err
}
var itemTtlMils uint32
if csr.TtlSeconds == 0 {
itemTtlMils = defaultTtlMils
} else {
itemTtlMils = ttlMils

}
request := pb.SetRequest{CacheKey: byteKey, CacheBody: byteValue, TtlMilliseconds: itemTtlMils}
ctx, cancel := context.WithTimeout(context.Background(), CacheCtxTimeout)
defer cancel()
md := createNewMetadata(csr.CacheName)
resp, errSet := dc.client.Set(metadata.NewOutgoingContext(ctx, md), &request)
if errSet != nil {
return nil, scserrors.GrpcErrorConverter(errSet)
}
newResp := responses.NewSetCacheResponse(resp)
return newResp, nil
}
return nil, scserrors.InvalidInputError("cache name cannot be empty")
request := pb.SetRequest{CacheKey: byteKey, CacheBody: byteValue, TtlMilliseconds: itemTtlMils}
ctx, cancel := context.WithTimeout(context.Background(), CacheCtxTimeout)
defer cancel()
md := createNewMetadata(csr.CacheName)
resp, err := dc.client.Set(metadata.NewOutgoingContext(ctx, md), &request)
if err != nil {
return nil, scserrors.GrpcErrorConverter(err)
}
newResp := responses.NewSetCacheResponse(resp)
return newResp, nil
}

func (dc *ScsDataClient) Get(cgr requests.CacheGetRequest) (*responses.GetCacheResponse, error) {
if utility.IsCacheNameValid(cgr.CacheName) {
byteKey, errAsBytes := asBytes(cgr.Key, "Unsupported type for key: ")
if errAsBytes != nil {
return nil, errAsBytes
}
request := pb.GetRequest{CacheKey: byteKey}
ctx, cancel := context.WithTimeout(context.Background(), CacheCtxTimeout)
defer cancel()
md := createNewMetadata(cgr.CacheName)
resp, getErr := dc.client.Get(metadata.NewOutgoingContext(ctx, md), &request)
if getErr != nil {
return nil, scserrors.GrpcErrorConverter(getErr)
}
newResp, er := responses.NewGetCacheResponse(resp)
if er != nil {
return nil, er
}
return newResp, nil
if !utility.IsCacheNameValid(cgr.CacheName) {
return nil, scserrors.InvalidInputError("cache name cannot be empty")
}
return nil, scserrors.InvalidInputError("cache name cannot be empty")
byteKey, err := asBytes(cgr.Key, "Unsupported type for key: ")
if err != nil {
return nil, err
}
request := pb.GetRequest{CacheKey: byteKey}
ctx, cancel := context.WithTimeout(context.Background(), CacheCtxTimeout)
defer cancel()
md := createNewMetadata(cgr.CacheName)
resp, err := dc.client.Get(metadata.NewOutgoingContext(ctx, md), &request)
if err != nil {
return nil, scserrors.GrpcErrorConverter(err)
}
newResp, err := responses.NewGetCacheResponse(resp)
if err != nil {
return nil, err
}
return newResp, nil

}

func asBytes(data interface{}, message string) ([]byte, error) {
Expand Down
25 changes: 13 additions & 12 deletions internal/tests/momento_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/google/uuid"
"github.com/momentohq/client-sdk-go/internal/scserrors"
"github.com/momentohq/client-sdk-go/momento"
"github.com/momentohq/client-sdk-go/momento/requests"
"github.com/momentohq/client-sdk-go/momento/responses"
Expand Down Expand Up @@ -37,9 +38,9 @@ func setUp(t *testing.T) (*momento.ScsClient, error) {
createCacheRequest := requests.CreateCacheRequest{
CacheName: TestCacheName,
}
createErr := client.CreateCache(createCacheRequest)
if !strings.Contains(createErr.Error(), "AlreadyExists") {
t.Error(createErr.Error())
err := client.CreateCache(createCacheRequest)
if !strings.Contains(err.Error(), scserrors.AlreadyExists) {
t.Error(err.Error())
}
return client, nil
}
Expand Down Expand Up @@ -68,28 +69,28 @@ func TestCreateCacheGetSetValueAndDeleteCache(t *testing.T) {
createCacheRequest := requests.CreateCacheRequest{
CacheName: cacheName,
}
createCacheErr := client.CreateCache(createCacheRequest)
if createCacheErr != nil {
t.Error(createCacheErr.Error())
err = client.CreateCache(createCacheRequest)
if err != nil {
t.Error(err.Error())
}

setRequest := requests.CacheSetRequest{
CacheName: cacheName,
Key: key,
Value: value,
}
_, setErr := client.Set(setRequest)
if setErr != nil {
t.Error(setErr.Error())
_, err = client.Set(setRequest)
if err != nil {
t.Error(err.Error())
}

getRequest := requests.CacheGetRequest{
CacheName: cacheName,
Key: key,
}
getResp, getErr := client.Get(getRequest)
if getErr != nil {
t.Error(getErr.Error())
getResp, err := client.Get(getRequest)
if err != nil {
t.Error(err.Error())
}
if getResp.Result() != responses.HIT {
t.Error("Cache miss")
Expand Down
12 changes: 6 additions & 6 deletions momento/simple_cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ func SimpleCacheClient(ccr requests.SimpleCacheClientRequest) (*ScsClient, error
AuthToken: ccr.AuthToken,
Endpoint: ctEndpoint,
}
controlClient, ctErr := scsmanagers.NewScsControlClient(controlClientRequest)
if ctErr != nil {
return nil, ctErr
controlClient, err := scsmanagers.NewScsControlClient(controlClientRequest)
if err != nil {
return nil, err
}
dataClientRequest := internalRequests.DataClientRequest{
AuthToken: ccr.AuthToken,
Endpoint: cEndpoint,
DefaultTtlSeconds: ccr.DefaultTtlSeconds,
}
dataClient, cErr := scsmanagers.NewScsDataClient(dataClientRequest)
if cErr != nil {
return nil, cErr
dataClient, err := scsmanagers.NewScsDataClient(dataClientRequest)
if err != nil {
return nil, err
}
return &ScsClient{authToken: ccr.AuthToken, defaultTtlSeconds: ccr.DefaultTtlSeconds, controlClient: controlClient, dataClient: dataClient}, nil
}
Expand Down

0 comments on commit d5bfcc2

Please sign in to comment.