2025-01-17 18:09:39 +05:30
|
|
|
package rediscache
|
2025-01-04 01:28:54 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2025-05-03 18:30:07 +05:30
|
|
|
"strings"
|
2025-01-04 01:28:54 +05:30
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-05-25 11:40:39 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/factory"
|
2025-07-05 19:08:23 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/instrumentation/instrumentationtest"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/types/cachetypes"
|
2025-05-03 18:30:07 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/valuer"
|
2025-01-04 01:28:54 +05:30
|
|
|
"github.com/go-redis/redismock/v8"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
type CacheableA struct {
|
2025-01-04 01:28:54 +05:30
|
|
|
Key string
|
|
|
|
|
Value int
|
|
|
|
|
Expiry time.Duration
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
func (cacheable *CacheableA) Clone() cachetypes.Cacheable {
|
|
|
|
|
return &CacheableA{
|
|
|
|
|
Key: cacheable.Key,
|
|
|
|
|
Value: cacheable.Value,
|
|
|
|
|
Expiry: cacheable.Expiry,
|
|
|
|
|
}
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
func (cacheable *CacheableA) MarshalBinary() ([]byte, error) {
|
|
|
|
|
return json.Marshal(cacheable)
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
func (cacheable *CacheableA) UnmarshalBinary(data []byte) error {
|
|
|
|
|
return json.Unmarshal(data, cacheable)
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
func TestSet(t *testing.T) {
|
2025-01-04 01:28:54 +05:30
|
|
|
db, mock := redismock.NewClientMock()
|
2025-07-05 19:08:23 +05:30
|
|
|
providerSettings := instrumentationtest.New().ToProviderSettings()
|
|
|
|
|
cache := &provider{client: db, settings: factory.NewScopedProviderSettings(providerSettings, "github.com/SigNoz/signoz/pkg/cache/rediscache")}
|
2025-01-04 01:28:54 +05:30
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
cacheable := &CacheableA{
|
2025-01-04 01:28:54 +05:30
|
|
|
Key: "some-random-key",
|
|
|
|
|
Value: 1,
|
|
|
|
|
Expiry: time.Microsecond,
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
orgID := valuer.GenerateUUID()
|
2025-07-05 19:08:23 +05:30
|
|
|
mock.ExpectSet(strings.Join([]string{orgID.StringValue(), "key"}, "::"), cacheable, 10*time.Second).SetVal("ok")
|
2025-01-04 01:28:54 +05:30
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
assert.NoError(t, cache.Set(context.Background(), orgID, "key", cacheable, 10*time.Second))
|
|
|
|
|
assert.NoError(t, mock.ExpectationsWereMet())
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|