aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorAlex Chen <Cnly@users.noreply.github.com>2019-08-06 23:33:53 +0800
committerGitHub <noreply@github.com>2019-08-06 23:33:53 +0800
commit66bf615360cfe6eac11e901ffe34f70f97330a22 (patch)
treef388b691f468c2198af2c6c6212fc6619c4b6f45 /common
parent83f8e05032b82e2dd85d93e77643139bb3231ba2 (diff)
Fix transaction IDs in transaction cache have global scope (#772)
Diffstat (limited to 'common')
-rw-r--r--common/transactions/transactions.go21
-rw-r--r--common/transactions/transactions_test.go35
2 files changed, 45 insertions, 11 deletions
diff --git a/common/transactions/transactions.go b/common/transactions/transactions.go
index febcb9a7..80b403a9 100644
--- a/common/transactions/transactions.go
+++ b/common/transactions/transactions.go
@@ -22,7 +22,14 @@ import (
// DefaultCleanupPeriod represents the default time duration after which cacheCleanService runs.
const DefaultCleanupPeriod time.Duration = 30 * time.Minute
-type txnsMap map[string]*util.JSONResponse
+type txnsMap map[CacheKey]*util.JSONResponse
+
+// CacheKey is the type for the key in a transactions cache.
+// This is needed because the spec requires transaction IDs to have a per-access token scope.
+type CacheKey struct {
+ AccessToken string
+ TxnID string
+}
// Cache represents a temporary store for response entries.
// Entries are evicted after a certain period, defined by cleanupPeriod.
@@ -50,14 +57,14 @@ func NewWithCleanupPeriod(cleanupPeriod time.Duration) *Cache {
return &t
}
-// FetchTransaction looks up an entry for txnID in Cache.
+// FetchTransaction looks up an entry for the (accessToken, txnID) tuple in Cache.
// Looks in both the txnMaps.
// Returns (JSON response, true) if txnID is found, else the returned bool is false.
-func (t *Cache) FetchTransaction(txnID string) (*util.JSONResponse, bool) {
+func (t *Cache) FetchTransaction(accessToken, txnID string) (*util.JSONResponse, bool) {
t.RLock()
defer t.RUnlock()
for _, txns := range t.txnsMaps {
- res, ok := txns[txnID]
+ res, ok := txns[CacheKey{accessToken, txnID}]
if ok {
return res, true
}
@@ -65,13 +72,13 @@ func (t *Cache) FetchTransaction(txnID string) (*util.JSONResponse, bool) {
return nil, false
}
-// AddTransaction adds an entry for txnID in Cache for later access.
+// AddTransaction adds an entry for the (accessToken, txnID) tuple in Cache.
// Adds to the front txnMap.
-func (t *Cache) AddTransaction(txnID string, res *util.JSONResponse) {
+func (t *Cache) AddTransaction(accessToken, txnID string, res *util.JSONResponse) {
t.Lock()
defer t.Unlock()
- t.txnsMaps[0][txnID] = res
+ t.txnsMaps[0][CacheKey{accessToken, txnID}] = res
}
// cacheCleanService is responsible for cleaning up entries after cleanupPeriod.
diff --git a/common/transactions/transactions_test.go b/common/transactions/transactions_test.go
index 0cdb776c..f565e484 100644
--- a/common/transactions/transactions_test.go
+++ b/common/transactions/transactions_test.go
@@ -24,27 +24,54 @@ type fakeType struct {
}
var (
- fakeTxnID = "aRandomTxnID"
- fakeResponse = &util.JSONResponse{Code: http.StatusOK, JSON: fakeType{ID: "0"}}
+ fakeAccessToken = "aRandomAccessToken"
+ fakeAccessToken2 = "anotherRandomAccessToken"
+ fakeTxnID = "aRandomTxnID"
+ fakeResponse = &util.JSONResponse{
+ Code: http.StatusOK, JSON: fakeType{ID: "0"},
+ }
+ fakeResponse2 = &util.JSONResponse{
+ Code: http.StatusOK, JSON: fakeType{ID: "1"},
+ }
)
// TestCache creates a New Cache and tests AddTransaction & FetchTransaction
func TestCache(t *testing.T) {
fakeTxnCache := New()
- fakeTxnCache.AddTransaction(fakeTxnID, fakeResponse)
+ fakeTxnCache.AddTransaction(fakeAccessToken, fakeTxnID, fakeResponse)
// Add entries for noise.
for i := 1; i <= 100; i++ {
fakeTxnCache.AddTransaction(
+ fakeAccessToken,
fakeTxnID+string(i),
&util.JSONResponse{Code: http.StatusOK, JSON: fakeType{ID: string(i)}},
)
}
- testResponse, ok := fakeTxnCache.FetchTransaction(fakeTxnID)
+ testResponse, ok := fakeTxnCache.FetchTransaction(fakeAccessToken, fakeTxnID)
if !ok {
t.Error("Failed to retrieve entry for txnID: ", fakeTxnID)
} else if testResponse.JSON != fakeResponse.JSON {
t.Error("Fetched response incorrect. Expected: ", fakeResponse.JSON, " got: ", testResponse.JSON)
}
}
+
+// TestCacheScope ensures transactions with the same transaction ID are not shared
+// across multiple access tokens.
+func TestCacheScope(t *testing.T) {
+ cache := New()
+ cache.AddTransaction(fakeAccessToken, fakeTxnID, fakeResponse)
+ cache.AddTransaction(fakeAccessToken2, fakeTxnID, fakeResponse2)
+
+ if res, ok := cache.FetchTransaction(fakeAccessToken, fakeTxnID); !ok {
+ t.Errorf("failed to retrieve entry for (%s, %s)", fakeAccessToken, fakeTxnID)
+ } else if res.JSON != fakeResponse.JSON {
+ t.Errorf("Wrong cache entry for (%s, %s). Expected: %v; got: %v", fakeAccessToken, fakeTxnID, fakeResponse.JSON, res.JSON)
+ }
+ if res, ok := cache.FetchTransaction(fakeAccessToken2, fakeTxnID); !ok {
+ t.Errorf("failed to retrieve entry for (%s, %s)", fakeAccessToken, fakeTxnID)
+ } else if res.JSON != fakeResponse2.JSON {
+ t.Errorf("Wrong cache entry for (%s, %s). Expected: %v; got: %v", fakeAccessToken, fakeTxnID, fakeResponse2.JSON, res.JSON)
+ }
+}