-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Path parameter support for async subscriptions (#326)
- Loading branch information
Showing
16 changed files
with
366 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cache | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"sync" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/serverless/event-gateway/functions" | ||
) | ||
|
||
type functionCache struct { | ||
sync.RWMutex | ||
cache map[functions.FunctionID]*functions.Function | ||
log *zap.Logger | ||
} | ||
|
||
func newFunctionCache(log *zap.Logger) *functionCache { | ||
return &functionCache{ | ||
cache: map[functions.FunctionID]*functions.Function{}, | ||
log: log, | ||
} | ||
} | ||
|
||
func (c *functionCache) Modified(k string, v []byte) { | ||
c.log.Debug("Function local cache received value update.", zap.String("key", k), zap.String("value", string(v))) | ||
|
||
f := &functions.Function{} | ||
err := json.NewDecoder(bytes.NewReader(v)).Decode(f) | ||
if err != nil { | ||
c.log.Error("Could not deserialize Function state.", zap.Error(err), zap.String("key", k), zap.String("value", string(v))) | ||
} else { | ||
c.Lock() | ||
defer c.Unlock() | ||
c.cache[functions.FunctionID(k)] = f | ||
} | ||
} | ||
|
||
func (c *functionCache) Deleted(k string, v []byte) { | ||
c.Lock() | ||
defer c.Unlock() | ||
delete(c.cache, functions.FunctionID(k)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/serverless/event-gateway/functions" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func TestFunctionCacheModified(t *testing.T) { | ||
fcache := newFunctionCache(zap.NewNop()) | ||
|
||
fcache.Modified("testfunc1", []byte(`{"functionId":"testfunc1"}`)) | ||
fcache.Modified("testfunc2", []byte(`{"functionId":"testfunc2"}`)) | ||
|
||
id1 := functions.FunctionID("testfunc1") | ||
id2 := functions.FunctionID("testfunc2") | ||
assert.Equal(t, &functions.Function{ID: id1}, fcache.cache[id1]) | ||
assert.Equal(t, &functions.Function{ID: id2}, fcache.cache[id2]) | ||
} | ||
|
||
func TestFunctionCacheModified_WrongPayload(t *testing.T) { | ||
fcache := newFunctionCache(zap.NewNop()) | ||
|
||
fcache.Modified("testfunc1", []byte(`not json`)) | ||
|
||
assert.Equal(t, map[functions.FunctionID]*functions.Function{}, fcache.cache) | ||
} | ||
|
||
func TestFunctionCacheModifiedDeleted(t *testing.T) { | ||
fcache := newFunctionCache(zap.NewNop()) | ||
|
||
fcache.Modified("testfunc1", []byte(`{"functionId":"testfunc1"}`)) | ||
fcache.Modified("testfunc2", []byte(`{"functionId":"testfunc2"}`)) | ||
fcache.Deleted("testfunc2", []byte(`{"functionId":"testfunc2"}`)) | ||
|
||
id1 := functions.FunctionID("testfunc1") | ||
assert.Equal(t, map[functions.FunctionID]*functions.Function{id1: &functions.Function{ID: id1}}, fcache.cache) | ||
} |
Oops, something went wrong.