-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_cache_test.go
More file actions
108 lines (88 loc) · 3.86 KB
/
Copy pathschema_cache_test.go
File metadata and controls
108 lines (88 loc) · 3.86 KB
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
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package dgdao_test
import (
"context"
"fmt"
"testing"
"github.com/dgraph-io/dgo/v250/protos/api"
"github.com/stretchr/testify/require"
dg "github.com/dgraph-io/dgdao"
)
type cachedNode struct {
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty" dgraph:"CachedNode"`
Name string `json:"cachedNodeName,omitempty" dgraph:"index=exact"`
}
// dropAllRaw drops all data and schema via a raw Alter on the underlying dgo
// client, bypassing Client.DropAll so the client's schema cache is NOT
// invalidated. Used to observe whether a write re-checks the schema.
func dropAllRaw(t *testing.T, client dg.Client) {
t.Helper()
raw, cleanup, err := client.DgraphClient()
require.NoError(t, err)
defer cleanup()
require.NoError(t, raw.Alter(context.Background(), &api.Operation{DropAll: true}))
}
// TestSchemaCache_SkipsValidationAfterFirstWrite proves the cache actually
// short-circuits the per-write schema fetch: after one successful insert, the
// schema is dropped out-of-band (bypassing DropAll's invalidation), and a
// second insert must NOT fail with "schema validation failed" — the check was
// served from cache rather than re-fetched.
func TestSchemaCache_SkipsValidationAfterFirstWrite(t *testing.T) {
client, err := dg.NewClient("file://" + GetTempDir(t)) // schema cache on by default
require.NoError(t, err)
defer client.Close()
ctx := context.Background()
require.NoError(t, client.UpdateSchema(ctx, &cachedNode{}))
require.NoError(t, client.Insert(ctx, &cachedNode{DType: []string{"CachedNode"}, Name: "warm"}))
dropAllRaw(t, client)
err = client.Insert(ctx, &cachedNode{DType: []string{"CachedNode"}, Name: "cached"})
if err != nil {
require.NotContains(t, err.Error(), "schema validation failed",
"cached type must skip the schema round-trip")
}
}
// TestSchemaCache_Disabled proves WithSchemaCache(false) preserves the
// original per-write behavior: the same out-of-band schema drop makes the
// very next insert fail validation.
func TestSchemaCache_Disabled(t *testing.T) {
client, err := dg.NewClient("file://"+GetTempDir(t), dg.WithSchemaCache(false))
require.NoError(t, err)
defer client.Close()
ctx := context.Background()
require.NoError(t, client.UpdateSchema(ctx, &cachedNode{}))
require.NoError(t, client.Insert(ctx, &cachedNode{DType: []string{"CachedNode"}, Name: "warm"}))
dropAllRaw(t, client)
err = client.Insert(ctx, &cachedNode{DType: []string{"CachedNode"}, Name: "unchecked"})
require.Error(t, err)
require.Contains(t, err.Error(), "schema validation failed")
}
// TestSchemaCache_DropAllInvalidates verifies that Client.DropAll flushes the
// cache: a type that validated before the drop must fail validation after it.
func TestSchemaCache_DropAllInvalidates(t *testing.T) {
client, err := dg.NewClient("file://" + GetTempDir(t))
require.NoError(t, err)
defer client.Close()
ctx := context.Background()
require.NoError(t, client.UpdateSchema(ctx, &cachedNode{}))
require.NoError(t, client.Insert(ctx, &cachedNode{DType: []string{"CachedNode"}, Name: "warm"}))
require.NoError(t, client.DropAll(ctx))
err = client.Insert(ctx, &cachedNode{DType: []string{"CachedNode"}, Name: "stale"})
require.Error(t, err)
require.Contains(t, err.Error(), "schema validation failed")
}
// TestSchemaCache_AutoSchema exercises the AutoSchema path with the cache on:
// repeated writes of the same type must keep working (the schema sync runs
// once and is skipped thereafter).
func TestSchemaCache_AutoSchema(t *testing.T) {
client, err := dg.NewClient("file://"+GetTempDir(t), dg.WithAutoSchema(true))
require.NoError(t, err)
defer client.Close()
ctx := context.Background()
for i := 0; i < 3; i++ {
require.NoError(t, client.Insert(ctx, &cachedNode{DType: []string{"CachedNode"}, Name: fmt.Sprintf("row-%d", i)}))
}
}