-
Notifications
You must be signed in to change notification settings - Fork 0
/
bug668_test.go
68 lines (51 loc) · 1.34 KB
/
bug668_test.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package bug668
import (
"log"
"testing"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/proto"
)
func TestDbBlock_packUnpack(t *testing.T) {
b1 := Block{
BlockId: "someid",
Timestamp: ptypes.TimestampNow(),
}
packed, err := packBlock(&b1)
if err != nil {
t.Fatal(err)
}
b2, err := unpackBlock(packed)
if err != nil {
t.Fatal(err)
}
if b2.BlockId != b1.BlockId || !proto.Equal(b1.Timestamp, b2.Timestamp) {
t.Fatal("blocks are different", b1, b2)
}
}
// block wrapper as possible additional meta info holder
type dbBlock struct {
*Block `protobuf:"bytes,1,opt,name=block" json:"block,omitempty"`
}
// fulfilling Message interface
func (m *dbBlock) Reset() { *m = dbBlock{} }
func (m *dbBlock) String() string { return proto.CompactTextString(m) }
func (*dbBlock) ProtoMessage() {}
func init() {
proto.RegisterType((*dbBlock)(nil), "bug668.dbBlock")
}
func unpackBlock(value []byte) (*Block, error) {
var dbBlock dbBlock
err := proto.Unmarshal(value, &dbBlock)
if err != nil {
log.Fatalf("cannot un-marshall block %v, error=%v", dbBlock.BlockId, err)
}
return dbBlock.Block, err
}
func packBlock(b *Block) ([]byte, error) {
dbBlock := dbBlock{b}
buf, err := proto.Marshal(&dbBlock)
if err != nil {
log.Fatalf("cannot marshall block %v, error=%v", dbBlock.BlockId, err)
}
return buf, err
}