File: cache_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (125 lines) | stat: -rw-r--r-- 2,933 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
package integration

import (
	"context"
	"strings"
	"sync/atomic"
	"testing"
	"time"

	"github.com/linode/linodego"
)

func TestCache_RegionList(t *testing.T) {
	validateResult := func(r []linodego.Region, err error) {
		if err != nil {
			t.Fatal(err)
		}

		if len(r) == 0 {
			t.Fatalf("expected a list of regions - %v", r)
		}
	}

	client, teardown := createTestClient(t, "fixtures/TestCache_RegionList")
	defer teardown()

	// Collect request number
	totalRequests := int64(0)

	client.OnBeforeRequest(func(request *linodego.Request) error {
		page := request.QueryParam.Get("page")
		if !strings.Contains(request.URL, "regions") || page != "1" {
			return nil
		}

		atomic.AddInt64(&totalRequests, 1)
		return nil
	})

	// Ensure that overrides work as intended
	client.SetGlobalCacheExpiration(0)

	// First request (no cache)
	validateResult(client.ListRegions(context.Background(), nil))

	// Second request (cached)
	validateResult(client.ListRegions(context.Background(), nil))

	// Clear cache
	client.InvalidateCache()

	// Third request (non-cached)
	validateResult(client.ListRegions(context.Background(), nil))

	// Invalidate the region response
	if err := client.InvalidateCacheEndpoint("/regions"); err != nil {
		t.Fatal(err)
	}

	// Fourth request (non-cached)
	validateResult(client.ListRegions(context.Background(), nil))

	// Fifth request (cache disabled)
	client.UseCache(false)
	validateResult(client.ListRegions(context.Background(), nil))

	// Sixth request (cache disabled)
	validateResult(client.ListRegions(context.Background(), nil))

	// Validate request count
	if totalRequests != 4 {
		t.Fatalf("expected 4 requests, got %d", totalRequests)
	}
}

func TestCache_Expiration(t *testing.T) {
	validateResult := func(r []linodego.LinodeKernel, err error) {
		if err != nil {
			t.Fatal(err)
		}

		if len(r) == 0 {
			t.Fatalf("expected a list of kernels - %v", r)
		}
	}

	client, teardown := createTestClient(t, "fixtures/TestCache_Expiration")
	defer teardown()

	// Collect request number
	totalRequests := int64(0)

	client.OnBeforeRequest(func(request *linodego.Request) error {
		page := request.QueryParam.Get("page")
		if !strings.Contains(request.URL, "kernels") || page != "1" {
			return nil
		}

		atomic.AddInt64(&totalRequests, 1)
		return nil
	})

	// First request (no cache)
	validateResult(client.ListKernels(context.Background(), nil))

	// Second request (cached)
	validateResult(client.ListKernels(context.Background(), nil))

	// Entries should expire immediately
	client.SetGlobalCacheExpiration(0)

	// Third request (non-cached)
	validateResult(client.ListKernels(context.Background(), nil))

	// Entries shouldn't expire
	client.SetGlobalCacheExpiration(time.Hour)

	// Fourth request (cached)
	validateResult(client.ListKernels(context.Background(), nil))

	// Validate request count
	if totalRequests != 2 {
		t.Fatalf("expected 2 requests, got %d", totalRequests)
	}
}