From 33708d48789850f80fa103fd94725656d1d9d726 Mon Sep 17 00:00:00 2001 From: Freddy Date: Thu, 23 Nov 2023 11:05:41 -0500 Subject: [PATCH] add other to misshandler --- cache_test.go | 42 +++++++++++++++++++++++++++++--- main/main.go | 3 ++- mocks/compressor_mock.go | 11 ++++----- mocks/processor_mock.go | 50 +++++++++++++++++++++++---------------- mocks/transformer_mock.go | 11 ++++----- 5 files changed, 81 insertions(+), 36 deletions(-) diff --git a/cache_test.go b/cache_test.go index c890cc1..3d83006 100644 --- a/cache_test.go +++ b/cache_test.go @@ -68,6 +68,7 @@ func (p *MyProcessor) CacheMissSolver(request *RequestEntry, Code: Status5xx, } } + return b, nil } func TestNew(t *testing.T) { @@ -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 { @@ -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 } diff --git a/main/main.go b/main/main.go index d1cf29b..7716d2e 100644 --- a/main/main.go +++ b/main/main.go @@ -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 } @@ -34,6 +34,7 @@ func main() { capacity, capFactor, ttl, + ttl, p, ) diff --git a/mocks/compressor_mock.go b/mocks/compressor_mock.go index 5eecd99..2bc7cce 100644 --- a/mocks/compressor_mock.go +++ b/mocks/compressor_mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.20.0. DO NOT EDIT. +// Code generated by mockery v2.36.0. DO NOT EDIT. package mocks @@ -125,13 +125,12 @@ func (_c *CompressorI_Decompress_Call) RunAndReturn(run func([]byte) ([]byte, er return _c } -type mockConstructorTestingTNewCompressorI interface { +// NewCompressorI creates a new instance of CompressorI. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewCompressorI(t interface { mock.TestingT Cleanup(func()) -} - -// NewCompressorI creates a new instance of CompressorI. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewCompressorI(t mockConstructorTestingTNewCompressorI) *CompressorI { +}) *CompressorI { mock := &CompressorI{} mock.Mock.Test(t) diff --git a/mocks/processor_mock.go b/mocks/processor_mock.go index 9f4065c..b8c78bc 100644 --- a/mocks/processor_mock.go +++ b/mocks/processor_mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.20.0. DO NOT EDIT. +// Code generated by mockery v2.36.0. DO NOT EDIT. package mocks @@ -20,23 +20,26 @@ func (_m *ProcessorI[K, T]) EXPECT() *ProcessorI_Expecter[K, T] { return &ProcessorI_Expecter[K, T]{mock: &_m.Mock} } -// CacheMissSolver provides a mock function with given fields: _a0 -func (_m *ProcessorI[K, T]) CacheMissSolver(_a0 K) (T, *models.RequestError) { - ret := _m.Called(_a0) +// CacheMissSolver provides a mock function with given fields: _a0, _a1 +func (_m *ProcessorI[K, T]) CacheMissSolver(_a0 K, _a1 ...interface{}) (T, *models.RequestError) { + var _ca []interface{} + _ca = append(_ca, _a0) + _ca = append(_ca, _a1...) + ret := _m.Called(_ca...) var r0 T var r1 *models.RequestError - if rf, ok := ret.Get(0).(func(K) (T, *models.RequestError)); ok { - return rf(_a0) + if rf, ok := ret.Get(0).(func(K, ...interface{}) (T, *models.RequestError)); ok { + return rf(_a0, _a1...) } - if rf, ok := ret.Get(0).(func(K) T); ok { - r0 = rf(_a0) + if rf, ok := ret.Get(0).(func(K, ...interface{}) T); ok { + r0 = rf(_a0, _a1...) } else { r0 = ret.Get(0).(T) } - if rf, ok := ret.Get(1).(func(K) *models.RequestError); ok { - r1 = rf(_a0) + if rf, ok := ret.Get(1).(func(K, ...interface{}) *models.RequestError); ok { + r1 = rf(_a0, _a1...) } else { if ret.Get(1) != nil { r1 = ret.Get(1).(*models.RequestError) @@ -53,13 +56,21 @@ type ProcessorI_CacheMissSolver_Call[K interface{}, T interface{}] struct { // CacheMissSolver is a helper method to define mock.On call // - _a0 K -func (_e *ProcessorI_Expecter[K, T]) CacheMissSolver(_a0 interface{}) *ProcessorI_CacheMissSolver_Call[K, T] { - return &ProcessorI_CacheMissSolver_Call[K, T]{Call: _e.mock.On("CacheMissSolver", _a0)} +// - _a1 ...interface{} +func (_e *ProcessorI_Expecter[K, T]) CacheMissSolver(_a0 interface{}, _a1 ...interface{}) *ProcessorI_CacheMissSolver_Call[K, T] { + return &ProcessorI_CacheMissSolver_Call[K, T]{Call: _e.mock.On("CacheMissSolver", + append([]interface{}{_a0}, _a1...)...)} } -func (_c *ProcessorI_CacheMissSolver_Call[K, T]) Run(run func(_a0 K)) *ProcessorI_CacheMissSolver_Call[K, T] { +func (_c *ProcessorI_CacheMissSolver_Call[K, T]) Run(run func(_a0 K, _a1 ...interface{})) *ProcessorI_CacheMissSolver_Call[K, T] { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(K)) + variadicArgs := make([]interface{}, len(args)-1) + for i, a := range args[1:] { + if a != nil { + variadicArgs[i] = a.(interface{}) + } + } + run(args[0].(K), variadicArgs...) }) return _c } @@ -69,7 +80,7 @@ func (_c *ProcessorI_CacheMissSolver_Call[K, T]) Return(_a0 T, _a1 *models.Reque return _c } -func (_c *ProcessorI_CacheMissSolver_Call[K, T]) RunAndReturn(run func(K) (T, *models.RequestError)) *ProcessorI_CacheMissSolver_Call[K, T] { +func (_c *ProcessorI_CacheMissSolver_Call[K, T]) RunAndReturn(run func(K, ...interface{}) (T, *models.RequestError)) *ProcessorI_CacheMissSolver_Call[K, T] { _c.Call.Return(run) return _c } @@ -126,13 +137,12 @@ func (_c *ProcessorI_ToMapKey_Call[K, T]) RunAndReturn(run func(K) (string, erro return _c } -type mockConstructorTestingTNewProcessorI interface { +// NewProcessorI creates a new instance of ProcessorI. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewProcessorI[K interface{}, T interface{}](t interface { mock.TestingT Cleanup(func()) -} - -// NewProcessorI creates a new instance of ProcessorI. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewProcessorI[K interface{}, T interface{}](t mockConstructorTestingTNewProcessorI) *ProcessorI[K, T] { +}) *ProcessorI[K, T] { mock := &ProcessorI[K, T]{} mock.Mock.Test(t) diff --git a/mocks/transformer_mock.go b/mocks/transformer_mock.go index f961475..163ad2d 100644 --- a/mocks/transformer_mock.go +++ b/mocks/transformer_mock.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.20.0. DO NOT EDIT. +// Code generated by mockery v2.36.0. DO NOT EDIT. package mocks @@ -123,13 +123,12 @@ func (_c *TransformerI_ValueToBytes_Call[T]) RunAndReturn(run func(T) ([]byte, e return _c } -type mockConstructorTestingTNewTransformerI interface { +// NewTransformerI creates a new instance of TransformerI. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewTransformerI[T interface{}](t interface { mock.TestingT Cleanup(func()) -} - -// NewTransformerI creates a new instance of TransformerI. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewTransformerI[T interface{}](t mockConstructorTestingTNewTransformerI) *TransformerI[T] { +}) *TransformerI[T] { mock := &TransformerI[T]{} mock.Mock.Test(t)