Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add concurrency test #8

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"context"
"encoding/json"
"errors"
"log"
"testing"
"time"

Expand Down Expand Up @@ -192,6 +193,94 @@
})
}

func TestConcurrentRequestsForSameKey(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: ":6379",
DB: 0,
})

subscriptionTimeout := time.Second * 5
keyTTL := time.Second * 2
requestProcessingTime := 100 * time.Millisecond

response := Response{Result: true}

// First request
resourceKey := "data"
go func() {
cache := cache_lib.NewCache[Response](redisClient, &cache_lib.CacheOptions{
SubscriptionTimeout: subscriptionTimeout,
UnsubscribeAndClose: true,
})
data, err := cache.RememberBlocking(
context.Background(),
func(ctx context.Context) (*Response, error) {
log.Println("miss from 1")
time.Sleep(requestProcessingTime)
return &response, nil

Check failure on line 220 in cache_test.go

View workflow job for this annotation

GitHub Actions / go-tests (1.18)

return with no blank line before (nlreturn)
},
func(ctx context.Context, data *Response) {
log.Println("hit from 1 request")
},
resourceKey,
keyTTL,
)
log.Println("first request finished", data, err)
}()

// Second request come with some delay. Key is existing but subscription missed event and stuck till subscriptionTimeout
time.Sleep(requestProcessingTime + time.Millisecond*1)

cache := cache_lib.NewCache[Response](redisClient, &cache_lib.CacheOptions{
SubscriptionTimeout: subscriptionTimeout,
UnsubscribeAndClose: true,
})
result, err := cache.RememberBlocking(context.Background(),
func(ctx context.Context) (*Response, error) {
log.Println("miss from 2")
time.Sleep(requestProcessingTime)
return &response, nil

Check failure on line 242 in cache_test.go

View workflow job for this annotation

GitHub Actions / go-tests (1.18)

return with no blank line before (nlreturn)
},
func(ctx context.Context, data *Response) {
log.Println("hit from 2 request")
},
resourceKey,
keyTTL)
log.Println("second request finished", result, err)
assert.NoError(t, err)
}

func TestPublishBeforeSubscribe(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: ":6379",
DB: 0,
})
_, err := redisClient.Publish(context.Background(), "foo", "before subscribe").Result()
assert.NoError(t, err)

subscription := cache_lib.NewCacheSubscription(redisClient, "foo")
subscription.Subscribe(context.Background())
channel, err := subscription.GetChannel(context.Background())
assert.NoError(t, err)
// Subscription timeout
go func() {
time.Sleep(time.Second * 5)
_ = subscription.UnsubscribeAndClose(context.Background())
}()
go func() {
_, err := redisClient.Publish(context.Background(), "foo", "publish immediately").Result()
assert.NoError(t, err)
}()
go func() {
time.Sleep(time.Millisecond * 10)
_, err := redisClient.Publish(context.Background(), "foo", "after subscribe").Result()
assert.NoError(t, err)
}()
for msg := range channel {
log.Println(msg.Payload)
}

}

Check failure on line 283 in cache_test.go

View workflow job for this annotation

GitHub Actions / go-tests (1.18)

unnecessary trailing newline (whitespace)
func TestCacheFailSet(t *testing.T) {
db, mock := redismock.NewClientMock()

Expand Down
Loading