service_internal_test.go 1.7 KB
Newer Older
H
Helin Wang 已提交
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
package pserver

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

const testDir = "./test_data"

type myKV struct {
	m map[string][]byte
}

func (m *myKV) GetKey(key string, timeout time.Duration) ([]byte, error) {
	if m.m == nil {
		m.m = make(map[string][]byte)
	}
	return m.m[key], nil
}

func (m *myKV) PutKey(key string, value []byte, timeout time.Duration, withLease bool) error {
	if m.m == nil {
		m.m = make(map[string][]byte)
	}
	m.m[key] = value
	return nil
}

func TestCheckpoint(t *testing.T) {
	kv := &myKV{}
	s, err := NewService(0, time.Hour, testDir, kv, nil)
	assert.Nil(t, err)
	err = s.checkpoint()
	assert.Nil(t, err)
	_, err = LoadCheckpoint(kv, 0)
	assert.Nil(t, err)
}

func float32ToByte(f float32) []byte {
	var buf bytes.Buffer
	err := binary.Write(&buf, binary.LittleEndian, f)
	if err != nil {
		fmt.Println("binary.Write failed:", err)
	}
	return buf.Bytes()
}

func TestCheckpointWithData(t *testing.T) {
	kv := &myKV{}
	s, err := NewService(0, time.Hour, testDir, kv, nil)
	assert.Nil(t, err)

	var content []byte
	for i := 0; i < 50000; i++ {
		content = append(content, float32ToByte(float32(i))...)
	}

	p1 := Parameter{Name: "p1", ElementType: 1, Content: content}
	err = s.InitParam(ParameterWithConfig{Param: p1}, nil)
	assert.Nil(t, err)

	err = s.FinishInitParams(0, nil)
	assert.Nil(t, err)

	var p2 Parameter
	err = s.GetParam(p1.Name, &p2)
	assert.Nil(t, err)
	assert.Equal(t, p1, p2)

	err = s.checkpoint()
	assert.Nil(t, err)
	cp, err := LoadCheckpoint(kv, 0)
	assert.Nil(t, err)
	s1, err := NewService(0, time.Hour, testDir, kv, cp)
	assert.Nil(t, err)

	var p3 Parameter
	err = s1.GetParam(p1.Name, &p3)
	assert.Nil(t, err)
	assert.Equal(t, p1, p3)
}