aboutsummaryrefslogtreecommitdiff
path: root/weed/filesys/dirty_page_interval_test.go
blob: d02ad27fdb4f1faee51a2e205b1e7efc069af15f (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package filesys

import (
	"bytes"
	"math/rand"
	"testing"
)

func TestContinuousIntervals_AddIntervalAppend(t *testing.T) {

	c := &ContinuousIntervals{}

	// 25, 25, 25
	c.AddInterval(getBytes(25, 3), 0)
	//  _,  _, 23, 23, 23, 23
	c.AddInterval(getBytes(23, 4), 2)

	expectedData(t, c, 0, 25, 25, 23, 23, 23, 23)

}

func TestContinuousIntervals_AddIntervalInnerOverwrite(t *testing.T) {

	c := &ContinuousIntervals{}

	// 25, 25, 25, 25, 25
	c.AddInterval(getBytes(25, 5), 0)
	//  _,  _, 23, 23
	c.AddInterval(getBytes(23, 2), 2)

	expectedData(t, c, 0, 25, 25, 23, 23, 25)

}

func TestContinuousIntervals_AddIntervalFullOverwrite(t *testing.T) {

	c := &ContinuousIntervals{}

	// 1,
	c.AddInterval(getBytes(1, 1), 0)
	//  _,  2,
	c.AddInterval(getBytes(2, 1), 1)
	//  _,  _,  3,  3, 3
	c.AddInterval(getBytes(3, 3), 2)
	//  _,  _,  _,  4, 4, 4
	c.AddInterval(getBytes(4, 3), 3)

	expectedData(t, c, 0, 1, 2, 3, 4, 4, 4)

}

func TestContinuousIntervals_RealCase1(t *testing.T) {

	c := &ContinuousIntervals{}

	// 25,
	c.AddInterval(getBytes(25, 1), 0)
	//  _,  _,  _,  _, 23, 23
	c.AddInterval(getBytes(23, 2), 4)
	//  _,  _,  _, 24, 24, 24, 24
	c.AddInterval(getBytes(24, 4), 3)

	//  _,  22, 22
	c.AddInterval(getBytes(22, 2), 1)

	expectedData(t, c, 0, 25, 22, 22, 24, 24, 24, 24)

}

func TestRandomWrites(t *testing.T) {

	c := &ContinuousIntervals{}

	data := make([]byte, 1024)

	for i := 0; i < 1024; i++ {

		start, stop := rand.Intn(len(data)), rand.Intn(len(data))
		if start > stop {
			start, stop = stop, start
		}

		rand.Read(data[start : stop+1])

		c.AddInterval(data[start:stop+1], int64(start))

		expectedData(t, c, 0, data...)

	}

}

func expectedData(t *testing.T, c *ContinuousIntervals, offset int, data ...byte) {
	start, stop := int64(offset), int64(offset+len(data))
	for _, list := range c.lists {
		nodeStart, nodeStop := max(start, list.Head.Offset), min(stop, list.Head.Offset+list.Size())
		if nodeStart < nodeStop {
			buf := make([]byte, nodeStop-nodeStart)
			list.ReadData(buf, nodeStart, nodeStop)
			if bytes.Compare(buf, data[nodeStart-start:nodeStop-start]) != 0 {
				t.Errorf("expected %v actual %v", data[nodeStart-start:nodeStop-start], buf)
			}
		}
	}
}

func getBytes(content byte, length int) []byte {
	data := make([]byte, length)
	for i := 0; i < length; i++ {
		data[i] = content
	}
	return data
}