File: disk_cache_test.go

package info (click to toggle)
rdap 0.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 608 kB
  • sloc: makefile: 4
file content (104 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download
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
// OpenRDAP
// Copyright 2017 Tom Harwood
// MIT License, see the LICENSE file.

package cache

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

func TestDiskCache(t *testing.T) {
	dir, err := ioutil.TempDir("", "test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	rdapDir := filepath.Join(dir, ".openrdap")
	
	m1 := NewDiskCache()
	m1.Dir = rdapDir

	m2 := NewDiskCache()
	m2.Dir = rdapDir

	asn1 := []byte(string("file 1"))
	asn2 := []byte(string("file 2"))

	if m1.State("asn.json") != Absent {
		t.Fatalf("asn.json expected absent in m1")
	} else if m2.State("asn.json") != Absent {
		t.Fatalf("asn.json expected absent in m2")
	}

	if err := m1.Save("asn.json", asn1); err != nil {
		t.Fatalf("Save failed: %s", err)
	}

	if m1.State("asn.json") != Good {
		t.Fatalf("asn.json expected good in m1")
	} else if m2.State("asn.json") != ShouldReload {
		t.Fatalf("asn.json expected shouldreload in m2")
	}

	loaded1, err := m1.Load("asn.json")
	loaded2, err := m2.Load("asn.json")

	if m1.State("asn.json") != Good {
		t.Fatalf("asn.json expected good in m1")
	} else if m2.State("asn.json") != Good {
		t.Fatalf("asn.json expected good in m2")
	}

	if bytes.Compare(loaded1, asn1) != 0 {
		t.Fatalf("loaded1(%v) != asn1(%v)", loaded1, asn1)
	} else if bytes.Compare(loaded2, asn1) != 0 {
		t.Fatalf("loaded2(%v) != asn1(%v)", loaded2, asn1)
	}

	time.Sleep(time.Second)

	if err := m2.Save("asn.json", asn2); err != nil {
		t.Fatalf("Save failed: %s", err)
	}

	if m1.State("asn.json") != ShouldReload {
		t.Fatalf("asn.json expected shouldreload in m1")
	} else if m2.State("asn.json") != Good {
		t.Fatalf("asn.json expected good in m2")
	}

	m1.Timeout = 0
	m2.Timeout = 0

	if m1.State("asn.json") != Expired {
		t.Fatal("m1 timeout broken")
	} else if m2.State("asn.json") != Expired {
		t.Fatal("m2 timeout broken")
	}

	m1.Timeout = time.Hour
	m2.Timeout = time.Hour

	loaded1, err = m1.Load("asn.json")
	loaded2, err = m2.Load("asn.json")

	if m1.State("asn.json") != Good {
		t.Fatalf("asn.json expected good in m1")
	} else if m2.State("asn.json") != Good {
		t.Fatalf("asn.json expected good in m2")
	}

	if bytes.Compare(loaded1, asn2) != 0 {
		t.Fatalf("loaded1(%v) != asn2(%v)", loaded1, asn2)
	} else if bytes.Compare(loaded2, asn2) != 0 {
		t.Fatalf("loaded2(%v) != asn2(%v)", loaded2, asn2)
	}
}