|
| 1 | +//go:build test_db_postgres || test_db_sqlite |
| 2 | + |
| 3 | +package graphdb |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// TestNodeIsPublicCacheInvalidation ensures that we invalidate correctly our |
| 12 | +// cache we use when determing if a node is public or not. |
| 13 | +func TestNodeIsPublicCacheInvalidation(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + ctx := t.Context() |
| 16 | + |
| 17 | + graph := MakeTestGraph(t) |
| 18 | + |
| 19 | + node1 := createTestVertex(t) |
| 20 | + node2 := createTestVertex(t) |
| 21 | + |
| 22 | + require.NoError(t, graph.AddNode(ctx, node1)) |
| 23 | + require.NoError(t, graph.AddNode(ctx, node2)) |
| 24 | + |
| 25 | + edge, _ := createEdge(10, 0, 0, 0, node1, node2) |
| 26 | + require.NoError(t, graph.AddChannelEdge(ctx, &edge)) |
| 27 | + |
| 28 | + // First IsPublic call should populate cache. |
| 29 | + isPublic1, err := graph.IsPublicNode(node1.PubKeyBytes) |
| 30 | + require.NoError(t, err) |
| 31 | + require.True(t, isPublic1) |
| 32 | + |
| 33 | + // Test invalidation scenarios: |
| 34 | + |
| 35 | + // 1. DeleteChannelEdges: |
| 36 | + // Above, the channel being public should be cached, but we expect that |
| 37 | + // DeleteChannelEdge will invalidate the cache for both nodes else when |
| 38 | + // we call IsPublic, we will hit the cache. |
| 39 | + err = graph.DeleteChannelEdges(false, true, edge.ChannelID) |
| 40 | + require.NoError(t, err) |
| 41 | + isPublic1, err = graph.IsPublicNode(node1.PubKeyBytes) |
| 42 | + require.NoError(t, err) |
| 43 | + require.False(t, isPublic1) |
| 44 | + |
| 45 | + isPublic2, err := graph.IsPublicNode(node2.PubKeyBytes) |
| 46 | + require.NoError(t, err) |
| 47 | + require.False(t, isPublic2) |
| 48 | + |
| 49 | + // 2. AddChannelEdge: |
| 50 | + // Now we know that the last `IsPublicNode` call above will cache our |
| 51 | + // nodes with `isPublic` = false. But add a new channel edge should |
| 52 | + // invalidate the cache such that when we call `IsPublic` it should |
| 53 | + // return `True`. |
| 54 | + edge2, _ := createEdge(10, 1, 0, 1, node1, node2) |
| 55 | + require.NoError(t, graph.AddChannelEdge(ctx, &edge2)) |
| 56 | + isPublic1, err = graph.IsPublicNode(node1.PubKeyBytes) |
| 57 | + require.NoError(t, err) |
| 58 | + require.True(t, isPublic1) |
| 59 | + |
| 60 | + isPublic2, err = graph.IsPublicNode(node2.PubKeyBytes) |
| 61 | + require.NoError(t, err) |
| 62 | + require.True(t, isPublic2) |
| 63 | + |
| 64 | + // 3. DeleteNode: |
| 65 | + // Again, the last two sets of `IsPublic` should have cached our nodes |
| 66 | + // as `True`. Now we can delete a node and expect the next call to be |
| 67 | + // False. |
| 68 | + // |
| 69 | + // NOTE: We don't get an error calling `IsPublicNode` because of how the |
| 70 | + // SQL query is implemented to check for the existence of public nodes. |
| 71 | + require.NoError(t, graph.DeleteNode(ctx, node1.PubKeyBytes)) |
| 72 | + isPublic1, err = graph.IsPublicNode(node1.PubKeyBytes) |
| 73 | + require.NoError(t, err) |
| 74 | + require.False(t, isPublic1) |
| 75 | +} |
0 commit comments