-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinary_compression_test.go
More file actions
46 lines (38 loc) · 1.06 KB
/
binary_compression_test.go
File metadata and controls
46 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package example
import (
"bytes"
"context"
"fmt"
"time"
"github.com/abema/crema"
"github.com/abema/crema/ext/protobuf"
"google.golang.org/protobuf/types/known/wrapperspb"
)
func ExampleNewBinaryCompressionCodec() {
provider := &byteProvider{items: make(map[string][]byte)}
protobufCodec, err := protobuf.NewProtobufCodec(&wrapperspb.BytesValue{})
if err != nil {
fmt.Println(err)
return
}
codec := crema.NewBinaryCompressionCodec(protobufCodec, 0)
cache := crema.NewCache(provider, codec)
payload := bytes.Repeat([]byte("a"), 128)
value, err := cache.GetOrLoad(context.Background(), "blob", time.Minute, func(ctx context.Context) (*wrapperspb.BytesValue, error) {
return &wrapperspb.BytesValue{Value: payload}, nil
})
if err != nil {
fmt.Println(err)
return
}
provider.mu.Lock()
stored := append([]byte(nil), provider.items["blob"]...)
provider.mu.Unlock()
fmt.Println(bytes.Equal(value.Value, payload))
fmt.Println(stored[0] == crema.CompressionTypeIDZlib)
fmt.Println(len(stored[1:]) < len(payload))
// Output:
// true
// true
// true
}