File: cache_test.go

package info (click to toggle)
golang-github-cznic-fileutil 0.0~git20150708.0.1c9c88f-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 316 kB
  • sloc: makefile: 21
file content (83 lines) | stat: -rw-r--r-- 1,495 bytes parent folder | download | duplicates (4)
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
// Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// blame: jnml, labs.nic.cz

package storage

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

func newfile(t *testing.T) (string, string, Accessor) {
	dir, err := ioutil.TempDir("", "test-storage-")
	if err != nil {
		panic(err)
	}

	name := filepath.Join(dir, "test.tmp")
	f, err := NewFile(name, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
	if err != nil {
		t.Fatal("newfile", err)
	}

	return dir, name, f
}

func readfile(t *testing.T, name string) (b []byte) {
	var err error
	if b, err = ioutil.ReadFile(name); err != nil {
		t.Fatal("readfile")
	}

	return
}

func newcache(t *testing.T) (dir, name string, c *Cache) {
	dir, name, f := newfile(t)
	var err error
	if c, err = NewCache(f, 1<<20, nil); err != nil {
		t.Fatal("newCache", err)
	}

	return
}

func TestCache0(t *testing.T) {
	dir, name, c := newcache(t)
	defer os.RemoveAll(dir)

	if err := c.Close(); err != nil {
		t.Fatal(10, err)
	}

	if b := readfile(t, name); len(b) != 0 {
		t.Fatal(20, len(b), 0)
	}
}

func TestCache1(t *testing.T) {
	dir, name, c := newcache(t)
	defer os.RemoveAll(dir)

	if n, err := c.WriteAt([]byte{0xa5}, 0); n != 1 {
		t.Fatal(20, n, err)
	}

	if err := c.Close(); err != nil {
		t.Fatal(10, err)
	}

	b := readfile(t, name)
	if len(b) != 1 {
		t.Fatal(30, len(b), 1)
	}

	if b[0] != 0xa5 {
		t.Fatal(40, b[0], 0xa5)
	}
}