示例#1
0
func BenchmarkLogFileBuffer(b *testing.B) {
	name, err := rand.FileName("BenchmarkLogFile", ".log", 10)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + name
	file, err := os.Create(name)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	defer os.Remove(name)
	defer file.Close()

	back := NewOutBuffer(
		NewWriter(file).F(DefFormatter),
		b.N/2,
	)
	defer back.(*OutBuffer).Close()

	logger := New(back, false).Domain("test")

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		logger.Print(msg)
		b.SetBytes(l)
	}
}
示例#2
0
func BenchmarkBoltDbBuffer(b *testing.B) {
	name, err := rand.FileName("boltdb", ".db", 10)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + "/" + name
	gob := &Gob{
		TypeName: types.Name(&log{}),
	}
	bolt, err := NewBoltDb("test", name, 0600, nil, gob, gob)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	logger := New(
		NewOutBuffer(
			NewGeneric(bolt).F(DefFormatter),
			b.N/2,
		),
		false,
	).Domain("test")
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		logger.Print(msg)
		b.SetBytes(l)
	}
}
示例#3
0
func BenchmarkLogOuterFile(b *testing.B) {
	name, err := rand.FileName("BenchmarkLogFile", ".log", 10)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + "/" + name
	file, err := os.Create(name)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	defer os.Remove(name)
	defer file.Close()

	backend := NewWriter(file).F(DefFormatter)
	olog, ok := backend.(OuterLogger)
	if !ok {
		return
	}
	w := olog.OuterLog(DebugPrio, "tag")
	defer olog.Close()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		n, err := w.Write([]byte(msg + "\n"))
		if err != nil {
			b.Error(e.Trace(e.Forward(err)))
		}
		if int64(n) != l+1 {
			b.Error("write failed", n, l)
		}
		b.SetBytes(l)
	}
}
示例#4
0
func TestBoltDb(t *testing.T) {
	name, err := rand.FileName("boltdb", ".db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + "/" + name
	gob := &Gob{
		TypeName: types.Name(&TestStruct{I: 0}),
	}
	addstore(t, NewBoltDb, "test", name, os.FileMode(0600), &bolt.Options{}, gob, gob)
}
示例#5
0
func TestNewCacheDisk(t *testing.T) {
	var err error

	fn, err := rand.FileName("cron_", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	file := os.TempDir() + fn

	cd, err := NewCacheDisk(file, 0600, nil, 5)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	cache = NewCache(&photos{}, cd)
}
示例#6
0
文件: unix_test.go 项目: fcavani/ping
func TestUnix(t *testing.T) {
	name, err := rand.FileName("test", "socket", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	//name = os.TempDir() + name
	defer os.Remove(name)
	sig := make(chan struct{})
	go func() {
		t.Log("Listen")
		addr, err := net.ResolveUnixAddr("unix", name)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		ln, err := net.ListenUnix("unix", addr)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		sig <- struct{}{}
		t.Log("Accepting connections on", ln.Addr())
		conn, err := ln.Accept()
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		t.Log("Connection accepted.", conn.RemoteAddr())
		time.Sleep(100 * time.Millisecond)
		err = conn.Close()
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}()
	unixUrl := "socket://" + name
	t.Log(unixUrl)
	url := testParse(t, unixUrl)
	<-sig
	err = PingUnix(url)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#7
0
func BenchmarkLogfmt(b *testing.B) {
	name, err := rand.FileName("BenchmarkLogFile", ".log", 10)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	name = os.TempDir() + name
	file, err := os.Create(name)
	if err != nil {
		b.Error(e.Trace(e.Forward(err)))
	}
	defer os.Remove(name)
	defer file.Close()

	logger := New(
		NewLogfmt(file),
		false,
	).Domain("test")

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		logger.Print(msg)
	}
}
示例#8
0
func TestCursorSkipNobucket(t *testing.T) {
	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 3,
		}
		err := c.Init()
		if err != nil {
			return e.Forward(err)
		}
		c.Skip(3)
		if err := c.Err(); err != nil {
			return e.Forward(err)
		}
		return nil
	})
	if err != nil && !e.Equal(err, ErrInvBucket) {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#9
0
func TestCursorSeek(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key1")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key2")}, []byte("12")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key3")}, []byte("13")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key1")}, []byte("21")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key2")}, []byte("22")},
		{[]byte("test_bucket"), [][]byte{[]byte("key3"), []byte("key1")}, []byte("31")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 2,
		}
		err := c.Init()
		if err != nil {
			return e.Forward(err)
		}
		for i, d := range data {
			k, v := c.Seek(d.Keys...)
			if k == nil {
				return e.New("key not found")
			}
			for j, key := range k {
				if !bytes.Equal(key, data[i].Keys[j]) {
					return e.New("key is not equal %v %v %v", i, string(key), string(data[i].Keys[j]))
				}
			}
			if !bytes.Equal(v, data[i].Data) {
				return e.New("not equal %v", string(v))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	data2 := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key1")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key4")}, []byte("21")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key3")}, []byte("31")},
		{[]byte("test_bucket"), [][]byte{[]byte("key4"), []byte("key1")}, nil},
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 2,
		}
		err := c.Init()
		if err != nil {
			return e.Forward(err)
		}
		for i, d := range data2 {
			k, v := c.Seek(d.Keys...)
			if k == nil && d.Data == nil {
				continue
			} else if k == nil && d.Data != nil {
				return e.New("key not found %v", i)
			}
			if !bytes.Equal(v, data2[i].Data) {
				return e.New("not equal %v %v %v", i, string(v), string(data2[i].Data))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

}
示例#10
0
func TestIndex(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket1"), [][]byte{[]byte("key1")}, []byte("lorem")},
		{[]byte("test_bucket2"), [][]byte{[]byte("key1"), []byte("key2")}, []byte("datadatadatadatadata")},
		{[]byte("test_bucket2"), [][]byte{[]byte("key1"), []byte("key3")}, []byte("3")},
		{[]byte("test_bucket3"), [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")}, []byte("catoto")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		for i, d := range data {
			data, err := Get(tx, d.Bucket, d.Keys)
			if err != nil {
				return e.Push(err, e.New("Fail to get %v", i))
			}
			if !bytes.Equal(data, d.Data) {
				return e.New("not equal %v", i)
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		err = Del(tx, data[0].Bucket, data[0].Keys)
		if err != nil {
			return e.Push(err, e.New("Fail to del %v", 0))
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		for i, d := range data {
			data, err := Get(tx, d.Bucket, d.Keys)
			if i == 0 {
				if err != nil && !e.Equal(err, ErrKeyNotFound) {
					return e.Push(err, "fail with the wrong error")
				} else if err == nil {
					return e.New("not fail")
				}
				continue
			}
			if err != nil {
				return e.Push(err, e.New("Fail to get %v", i))
			}
			if !bytes.Equal(data, d.Data) {
				return e.New("not equal %v", i)
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data[1:] {
			err := Del(tx, d.Bucket, d.Keys)
			if err != nil {
				return e.Push(err, e.New("Fail to del %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		for i, d := range data[1:] {
			_, err := Get(tx, d.Bucket, d.Keys)
			if err != nil && !e.Equal(err, ErrKeyNotFound) {
				return e.Push(err, e.New("Fail to get %v", i))
			} else if err == nil {
				return e.New("nil error")
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#11
0
func TestDel(t *testing.T) {
	buckets := []string{"test_del"}
	data := []testData{
		{[]byte("test_del"), [][]byte{[]byte("key-a1"), []byte("key-b1"), []byte("key-c1")}, []byte("epson")},
		{[]byte("test_del"), [][]byte{[]byte("key-a2"), []byte("key-b2"), []byte("key-c2")}, []byte("catoto")},
		{[]byte("test_del"), [][]byte{[]byte("key-a3"), []byte("key-b3"), []byte("key-c3")}, []byte("catoto")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = PrintDb(db, buckets)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Del(tx, d.Bucket, d.Keys)
			if err != nil {
				PrintDbTx(tx, buckets)
				return e.Push(err, e.New("Fail to del %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = PrintDb(db, buckets)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = DbEmpty(db, buckets)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#12
0
func TestCursorRevSkipKeys(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key1")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key2")}, []byte("12")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key3")}, []byte("13")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key1")}, []byte("21")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key2")}, []byte("22")},
		{[]byte("test_bucket"), [][]byte{[]byte("key3"), []byte("key1")}, []byte("31")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 2,
			Reverse: true,
		}
		err := c.Init([]byte("key2"))
		if err != nil {
			return e.Forward(err)
		}

		//Next
		i := 4
		for k, v := c.First(); k != nil; k, v = c.Next() {
			if !bytes.Equal(v, data[i].Data) {
				return e.New("not equal %v", string(v))
			}
			for j, key := range k {
				if !bytes.Equal(key, data[i].Keys[j]) {
					return e.New("key is not equal %v %v", string(key), string(data[i].Keys[j]))
				}
			}
			i--
		}
		t.Log(i)
		if i > 2 {
			t.Fatal("iterator didn't stop", i)
		}

		//Prev
		i = 3
		for k, v := c.Last(); k != nil; k, v = c.Prev() {
			if !bytes.Equal(v, data[i].Data) {
				return e.New("not equal %v", string(v))
			}
			for j, key := range k {
				if !bytes.Equal(key, data[i].Keys[j]) {
					return e.New("key is not equal %v %v", string(key), string(data[i].Keys[j]))
				}
			}
			i++
		}
		t.Log(i)
		if i < 5 {
			t.Fatal("iterator didn't stop", i)
		}

		//Skip
		var m uint64
		var j int = 4
		for ; j < 2; j-- {
			keys, val := c.Skip(m)
			if err := c.Err(); err != nil {
				return e.Forward(err)
			}
			if keys == nil {
				return e.New("skip returned nil")
			}
			if !bytes.Equal(val, data[j].Data) {
				return e.New("not equal %v %v %v", m, string(val), string(data[j].Data))
			}
			for l, key := range keys {
				if !bytes.Equal(key, data[j].Keys[l]) {
					return e.New("key is not equal %v %v", string(key), string(data[j].Keys[l]))
				}
			}
			m++
		}

		//Seek
		// k, v := c.Seek(data[0].Keys...)
		// if k != nil {
		// 	return e.New("must be nil")
		// }

		k, v := c.Seek([]byte("bú"), []byte("key3"))
		if k == nil {
			return e.New("key not found")
		}
		t.Log(string(v))
		if !bytes.Equal(v, data[4].Data) {
			return e.New("not equal %v", string(v))
		}

		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#13
0
func TestCursorRevSkip(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key1")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key2")}, []byte("12")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key3")}, []byte("13")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key1")}, []byte("21")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key2")}, []byte("22")},
		{[]byte("test_bucket"), [][]byte{[]byte("key3"), []byte("key1")}, []byte("31")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("test_bucket"))
		buf := b.Get([]byte("key1"))
		if buf == nil {
			return e.New("key not found")
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 2,
			Reverse: true,
		}
		c.Init()

		var i uint64
		for ; i < uint64(len(data))-1; i++ {
			err := skiprev(i, c, data)
			if err != nil {
				return e.Forward(err)
			}
		}

		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

}
示例#14
0
func TestCursorRevPrev(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key1")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key2")}, []byte("12")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key3")}, []byte("13")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key1")}, []byte("21")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key2")}, []byte("22")},
		{[]byte("test_bucket"), [][]byte{[]byte("key3"), []byte("key1")}, []byte("31")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 2,
			Reverse: true,
		}
		c.Init()
		i := 0
		for k, v := c.Last(); k != nil; k, v = c.Prev() {
			if !bytes.Equal(v, data[i].Data) {
				return e.New("not equal %v", string(v))
			}
			for j, key := range k {
				if !bytes.Equal(key, data[i].Keys[j]) {
					return e.New("key is not equal %v %v", string(key), string(data[i].Keys[j]))
				}
			}
			i++
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#15
0
func TestCursorFirstLast(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key1")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key2")}, []byte("12")},
		{[]byte("test_bucket"), [][]byte{[]byte("key1"), []byte("key3")}, []byte("13")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key1")}, []byte("21")},
		{[]byte("test_bucket"), [][]byte{[]byte("key2"), []byte("key2")}, []byte("22")},
		{[]byte("test_bucket"), [][]byte{[]byte("key3"), []byte("key1")}, []byte("31")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 2,
		}
		err := c.Init()
		if err != nil {
			return e.Forward(err)
		}

		keys, val := c.First()
		if keys == nil {
			return e.New("First returned nil")
		}
		if !bytes.Equal(val, data[0].Data) {
			return e.New("not equal %v %v", string(val), string(data[0].Data))
		}
		for i, key := range keys {
			if !bytes.Equal(key, data[0].Keys[i]) {
				return e.New("key is not equal %v %v", string(key), string(data[0].Keys[i]))
			}
		}

		keys, val = c.Last()
		if keys == nil {
			return e.New("First returned nil")
		}
		l := len(data) - 1
		if !bytes.Equal(val, data[l].Data) {
			return e.New("not equal %v", string(val))
		}
		for i, key := range keys {
			if !bytes.Equal(key, data[l].Keys[i]) {
				return e.New("key is not equal %v %v", string(key), string(data[l].Keys[i]))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#16
0
func TestCursorBigIndexNextPrev(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte{'0'}, []byte("pt-br"), EncInt(2015), EncInt(1), EncInt(4), EncInt(14), EncInt(58), EncInt(59), []byte("Log")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte{'1'}, []byte("pt-br"), EncInt(2015), EncInt(12), EncInt(23), EncInt(17), EncInt(25), EncInt(59), []byte("Sem assunto e sem nome")}, []byte("12")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 9,
		}
		err := c.Init()
		if err != nil {
			return e.Forward(err)
		}
		i := 0
		for k, v := c.First(); k != nil; k, v = c.Next() {
			if err := c.Err(); err != nil {
				return e.Forward(err)
			}
			if !bytes.Equal(v, data[i].Data) {
				return e.New("not equal %v %v", i, string(v))
			}
			for j, key := range k {
				if !bytes.Equal(key, data[i].Keys[j]) {
					return e.New("key is not equal %v %v %v", i, string(key), string(data[i].Keys[j]))
				}
			}
			i++
		}
		i = len(data) - 1
		for k, v := c.Last(); k != nil; k, v = c.Prev() {
			if err := c.Err(); err != nil {
				return e.Forward(err)
			}
			if !bytes.Equal(v, data[i].Data) {
				return e.New("not equal %v %v", i, string(v))
			}
			for j, key := range k {
				if !bytes.Equal(key, data[i].Keys[j]) {
					return e.New("key is not equal %v %v %v", i, string(key), string(data[i].Keys[j]))
				}
			}
			i--
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#17
0
func TestCursorBigIndexSeek(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte{'0'}, []byte("pt-br"), EncInt(2015), EncInt(1), EncInt(4), EncInt(14), EncInt(58), EncInt(59), []byte("Log")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte{'1'}, []byte("pt-br"), EncInt(2015), EncInt(12), EncInt(23), EncInt(17), EncInt(25), EncInt(59), []byte("Sem assunto e sem nome")}, []byte("12")},
	}

	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = db.View(func(tx *bolt.Tx) error {
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 9,
		}
		err := c.Init()
		if err != nil {
			return e.Forward(err)
		}
		_, v := c.Seek([]byte{'1'}, []byte("pt-br"), EncInt(2015), EncInt(12), EncInt(23), EncInt(17), EncInt(25), EncInt(59), []byte("Sem assunto e sem nome"))
		if !bytes.Equal(v, []byte("12")) {
			t.Fatal("seek fail", string(v))
		}
		t.Log(string(v))
		// _, v = c.Next()
		// if v != nil {
		// 	t.Fatal("next fail: not nil")
		// }
		_, v = c.Prev()
		if v == nil {
			t.Fatal("prev is nil")
		}
		if !bytes.Equal(v, []byte("11")) {
			t.Fatal("prev not equal", string(v))
		}
		_, v = c.Prev()
		if v != nil {
			t.Fatal("prev fail: not nil")
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#18
0
func TestCursorReinsert(t *testing.T) {
	data := []testData{
		{[]byte("test_bucket"), [][]byte{[]byte{'0'}, []byte("pt-br"), EncInt(2015), EncInt(1), EncInt(4), EncInt(14), EncInt(58), EncInt(59), []byte("Log")}, []byte("11")},
		{[]byte("test_bucket"), [][]byte{[]byte{'1'}, []byte("pt-br"), EncInt(2015), EncInt(12), EncInt(23), EncInt(17), EncInt(25), EncInt(59), []byte("Sem assunto e sem nome")}, []byte("12")},
	}
	filename, err := rand.FileName("blog-", "db", 10)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	dir, err := ioutil.TempDir("", "blog-")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	db, err := bolt.Open(filepath.Join(dir, filename), 0600, nil)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = db.Update(func(tx *bolt.Tx) error {
		for i, d := range data {
			err := Put(tx, d.Bucket, d.Keys, d.Data)
			if err != nil {
				return e.Push(err, e.New("Fail to put %v", i))
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = db.Update(func(tx *bolt.Tx) error {
		d := data[0]
		err := Del(tx, d.Bucket, d.Keys)
		if err != nil {
			return e.Forward(err)
		}
		err = Put(tx, d.Bucket, d.Keys, d.Data)
		if err != nil {
			return e.Forward(err)
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = db.Update(func(tx *bolt.Tx) error {
		d := data[0]
		_, err := Get(tx, d.Bucket, d.Keys)
		if err != nil {
			return e.Forward(err)
		}
		c := &Cursor{
			Tx:      tx,
			Bucket:  []byte("test_bucket"),
			NumKeys: 9,
		}
		err = c.Init([]byte{'0'}, []byte("pt-br"))
		if err != nil {
			return e.Forward(err)
		}
		k, v := c.First()
		if k == nil {
			return e.New("can't get the first record")
		}
		if !bytes.Equal(v, d.Data) {
			return e.New("not equal %v", string(v))
		}
		return nil
	})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}