Skip to content

Commit ea6088c

Browse files
committed
Add test for new Evict method
1 parent 7ad7240 commit ea6088c

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

internal/workflow/cache/cache.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"github.com/jellydator/ttlcache/v3"
1212
)
1313

14-
type LruCache struct {
14+
type lruCache struct {
1515
mc metrics.Client
1616
c *ttlcache.Cache[string, workflow.WorkflowExecutor]
1717
}
1818

19-
func NewWorkflowExecutorLRUCache(mc metrics.Client, size int, expiration time.Duration) workflow.ExecutorCache {
19+
func NewWorkflowExecutorLRUCache(mc metrics.Client, size int, expiration time.Duration) *lruCache {
2020
c := ttlcache.New(
2121
ttlcache.WithCapacity[string, workflow.WorkflowExecutor](uint64(size)),
2222
ttlcache.WithTTL[string, workflow.WorkflowExecutor](expiration),
@@ -37,13 +37,13 @@ func NewWorkflowExecutorLRUCache(mc metrics.Client, size int, expiration time.Du
3737
mc.Counter(metrickeys.WorkflowInstanceCacheEviction, metrics.Tags{metrickeys.EvictionReason: reason}, 1)
3838
})
3939

40-
return &LruCache{
40+
return &lruCache{
4141
mc: mc,
4242
c: c,
4343
}
4444
}
4545

46-
func (lc *LruCache) Get(ctx context.Context, instance *core.WorkflowInstance) (workflow.WorkflowExecutor, bool, error) {
46+
func (lc *lruCache) Get(ctx context.Context, instance *core.WorkflowInstance) (workflow.WorkflowExecutor, bool, error) {
4747
e := lc.c.Get(getKey(instance))
4848
if e != nil {
4949
return e.Value(), true, nil
@@ -52,23 +52,23 @@ func (lc *LruCache) Get(ctx context.Context, instance *core.WorkflowInstance) (w
5252
return nil, false, nil
5353
}
5454

55-
func (lc *LruCache) Store(ctx context.Context, instance *core.WorkflowInstance, executor workflow.WorkflowExecutor) error {
55+
func (lc *lruCache) Store(ctx context.Context, instance *core.WorkflowInstance, executor workflow.WorkflowExecutor) error {
5656
lc.c.Set(getKey(instance), executor, ttlcache.DefaultTTL)
5757

5858
lc.mc.Gauge(metrickeys.WorkflowInstanceCacheSize, metrics.Tags{}, int64(lc.c.Len()))
5959

6060
return nil
6161
}
6262

63-
func (lc *LruCache) Evict(ctx context.Context, instance *core.WorkflowInstance) error {
63+
func (lc *lruCache) Evict(ctx context.Context, instance *core.WorkflowInstance) error {
6464
lc.c.Delete(getKey(instance))
6565

6666
lc.mc.Gauge(metrickeys.WorkflowInstanceCacheSize, metrics.Tags{}, int64(lc.c.Len()))
6767

6868
return nil
6969
}
7070

71-
func (lc *LruCache) StartEviction(ctx context.Context) {
71+
func (lc *lruCache) StartEviction(ctx context.Context) {
7272
go lc.c.Start()
7373

7474
<-ctx.Done()

internal/workflow/cache/cache_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Test_Cache_StoreAndGet(t *testing.T) {
5757
require.False(t, ok)
5858
}
5959

60-
func Test_Cache_Evict(t *testing.T) {
60+
func Test_Cache_AutoEviction(t *testing.T) {
6161
c := NewWorkflowExecutorLRUCache(
6262
metrics.NewNoopMetricsClient(),
6363
128,
@@ -87,6 +87,37 @@ func Test_Cache_Evict(t *testing.T) {
8787
require.Nil(t, e2)
8888
}
8989

90+
func Test_Cache_Evict(t *testing.T) {
91+
c := NewWorkflowExecutorLRUCache(
92+
metrics.NewNoopMetricsClient(),
93+
128,
94+
1, // Should evict immediately
95+
)
96+
97+
i := core.NewWorkflowInstance("instanceID", "executionID")
98+
r := wf.NewRegistry()
99+
r.RegisterWorkflow(workflowWithActivity)
100+
e, err := wf.NewExecutor(
101+
slog.Default(), trace.NewNoopTracerProvider().Tracer(backend.TracerName), r,
102+
converter.DefaultConverter, []workflow.ContextPropagator{}, &testHistoryProvider{}, i,
103+
&metadata.WorkflowMetadata{}, clock.New(),
104+
)
105+
require.NoError(t, err)
106+
107+
err = c.Store(context.Background(), i, e)
108+
require.NoError(t, err)
109+
110+
require.Equal(t, 1, c.c.Len())
111+
112+
require.NoError(t, c.Evict(context.Background(), i))
113+
require.Equal(t, 0, c.c.Len())
114+
115+
e2, ok, err := c.Get(context.Background(), i)
116+
require.NoError(t, err)
117+
require.False(t, ok)
118+
require.Nil(t, e2)
119+
}
120+
90121
func workflowWithActivity(ctx workflow.Context) (int, error) {
91122
r, err := workflow.ExecuteActivity[int](ctx, workflow.ActivityOptions{
92123
RetryOptions: workflow.RetryOptions{

0 commit comments

Comments
 (0)