Skip to content

Commit

Permalink
add other to misshandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Freddy committed Nov 23, 2023
1 parent 6fce91f commit 33708d4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 36 deletions.
42 changes: 39 additions & 3 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (p *MyProcessor) CacheMissSolver(request *RequestEntry,
Code: Status5xx,
}
}

return b, nil
}
func TestNew(t *testing.T) {
Expand Down Expand Up @@ -772,9 +773,44 @@ func TestTTLForNegative(t *testing.T) {
assert.False(contained)
}

//benchmark to test the performance of the cache
//when the processor should perform an addition task
type Other struct{}

func (p *Other) ToMapKey(entry string) (string, error) {
return entry, nil
}

func (p *Other) CacheMissSolver(entry string, other ...interface{}) (string, *models.RequestError) {
response, ok := other[0].(string)
if !ok {
return "", &models.RequestError{
Code: Status5xx,
}
}
return response, nil
}

func TestOtherInCache(t *testing.T) {
assert := assert.New(t)
p := &Other{}
cache := New[string, string](
Capacity,
.4,
2*time.Second,
time.Second,
p,
)
value, err := cache.RetrieveFromCacheOrCompute("1", "Keats")
assert.Nil(err)
assert.Equal("Keats", value)

value, err = cache.RetrieveFromCacheOrCompute("2", 0)
assert.NotNil(err)
assert.Equal(err.Code, Status5xx)
assert.Equal("", value)
}

// benchmark to test the performance of the cache
// when the processor should perform an addition task
type BenchProcessor struct{}

type Adder struct {
Expand All @@ -785,7 +821,7 @@ func (p *BenchProcessor) ToMapKey(adder Adder) (string, error) {
return fmt.Sprintf("%d+%d", adder.num1, adder.num2), nil
}

func (p *BenchProcessor) CacheMissSolver(adder Adder) (int, *models.RequestError) {
func (p *BenchProcessor) CacheMissSolver(adder Adder, _ ...interface{}) (int, *models.RequestError) {
return adder.num1 + adder.num2, nil
}

Expand Down
3 changes: 2 additions & 1 deletion main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (p *Processor) ToMapKey(someValue int) (string, error) {
}

// receive the value that will be used as a key and return a string, that will be used as a value
func (p *Processor) CacheMissSolver(someValue int) (string, *models.RequestError) {
func (p *Processor) CacheMissSolver(someValue int, _ ...interface{}) (string, *models.RequestError) {
time.Sleep(time.Second * 1)
return fmt.Sprintf("%d processed", someValue), nil
}
Expand All @@ -34,6 +34,7 @@ func main() {
capacity,
capFactor,
ttl,
ttl,
p,
)

Expand Down
11 changes: 5 additions & 6 deletions mocks/compressor_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 30 additions & 20 deletions mocks/processor_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions mocks/transformer_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 33708d4

Please sign in to comment.