mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
841c136e71
|
||
|
|
833517f35c
|
||
|
|
07e4f75a0a
|
@@ -92,7 +92,7 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
|
||||
queryHash = strutil.Md5(fmt.Sprintf("%s-%+v", query.compiledQuery, queryHeaders))
|
||||
cacheBaseClient.Logger.Debug(cacheBaseClient, "Hash calculated;", "hash:", queryHash)
|
||||
|
||||
cachedResponse = cacheBaseClient.cacheLookup(queryHash)
|
||||
cachedResponse = c.cacheLookup(queryHash)
|
||||
if cachedResponse != nil {
|
||||
cacheBaseClient.Logger.Debug(cacheBaseClient, "Found cached response")
|
||||
return cacheBaseClient.decodeResponse(cachedResponse), nil
|
||||
@@ -113,8 +113,12 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cacheBaseClient.cache.enabled {
|
||||
cacheBaseClient.cache.client.Set(queryHash, jsonData)
|
||||
if cacheBaseClient.cache.enabled && jsonData != nil && queryHash != "" {
|
||||
c.cache.client.Set(queryHash, jsonData)
|
||||
} else if jsonData == nil {
|
||||
cacheBaseClient.Logger.Warn(cacheBaseClient, "Response is empty")
|
||||
} else if queryHash == "" {
|
||||
cacheBaseClient.Logger.Warn(cacheBaseClient, "Query hash is empty")
|
||||
}
|
||||
|
||||
return cacheBaseClient.decodeResponse(jsonData), err
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@@ -413,3 +414,61 @@ func TestBaseClient_QueryCache(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBaseClient_QueryCacheRandomizedRace(t *testing.T) {
|
||||
type fields struct {
|
||||
graphql_endpoint string
|
||||
cache_enabled bool
|
||||
}
|
||||
type args struct {
|
||||
queryVariables interface{}
|
||||
queryHeaders map[string]interface{}
|
||||
queryContent string
|
||||
}
|
||||
tests := []struct {
|
||||
want any
|
||||
args args
|
||||
name string
|
||||
fields fields
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test QueryCache - enabled",
|
||||
fields: fields{
|
||||
graphql_endpoint: "https://spacex-production.up.railway.app/",
|
||||
},
|
||||
args: args{
|
||||
queryContent: `query Dragons {
|
||||
dragons {
|
||||
name
|
||||
}
|
||||
}`,
|
||||
queryHeaders: map[string]interface{}{
|
||||
"x-apollo-operation-name": "Missions-Potato-Test-Golang",
|
||||
"content-type": "application/json",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := NewConnection()
|
||||
if tt.fields.graphql_endpoint != "" {
|
||||
c.endpoint = tt.fields.graphql_endpoint
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
c.cache.enabled = rand.Intn(2) == 0
|
||||
go func() {
|
||||
_, err := c.Query(tt.args.queryContent, tt.args.queryVariables, tt.args.queryHeaders)
|
||||
if !tt.wantErr {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user