-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdevice_list_table_test.go
120 lines (107 loc) · 3.97 KB
/
device_list_table_test.go
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
package state
import (
"fmt"
"testing"
"github.com/matrix-org/sliding-sync/internal"
)
// Tests the DeviceLists table
func TestDeviceListTable(t *testing.T) {
db, close := connectToDB(t)
defer close()
table := NewDeviceListTable(db)
userID := "@TestDeviceListTable"
deviceID := "BOB"
// these are individual updates from Synapse from /sync v2
deltas := []internal.MapStringInt{
{
"alice": internal.DeviceListChanged,
},
{
"💣": internal.DeviceListChanged,
},
}
// apply them
for _, dd := range deltas {
err := table.Upsert(userID, deviceID, dd)
assertNoError(t, err)
}
// check we can read-only select. This doesn't modify any fields.
for i := 0; i < 3; i++ {
got, err := table.Select(userID, deviceID, false)
assertNoError(t, err)
// until we "swap" we don't consume the New entries
assertVal(t, "unexpected data on swapless select", got, internal.MapStringInt{})
}
// now swap-er-roo, which shifts everything from New into Sent.
got, err := table.Select(userID, deviceID, true)
assertNoError(t, err)
assertVal(t, "did not select what was upserted on swap select", got, internal.MapStringInt{
"alice": internal.DeviceListChanged,
"💣": internal.DeviceListChanged,
})
// this is permanent, read-only views show this too.
got, err = table.Select(userID, deviceID, false)
assertNoError(t, err)
assertVal(t, "swapless select did not return the same data as before", got, internal.MapStringInt{
"alice": internal.DeviceListChanged,
"💣": internal.DeviceListChanged,
})
// We now expect empty DeviceLists, as we swapped twice.
got, err = table.Select(userID, deviceID, true)
assertNoError(t, err)
assertVal(t, "swap select did not return nothing", got, internal.MapStringInt{})
// get back the original state
assertNoError(t, err)
for _, dd := range deltas {
err = table.Upsert(userID, deviceID, dd)
assertNoError(t, err)
}
// Move original state to Sent by swapping
got, err = table.Select(userID, deviceID, true)
assertNoError(t, err)
assertVal(t, "did not select what was upserted on swap select", got, internal.MapStringInt{
"alice": internal.DeviceListChanged,
"💣": internal.DeviceListChanged,
})
// Add new entries to New before acknowledging Sent
err = table.Upsert(userID, deviceID, internal.MapStringInt{
"💣": internal.DeviceListChanged,
"charlie": internal.DeviceListLeft,
})
assertNoError(t, err)
// Reading without swapping does not move New->Sent, so returns the previous value
got, err = table.Select(userID, deviceID, false)
assertNoError(t, err)
assertVal(t, "swapless select did not return the same data as before", got, internal.MapStringInt{
"alice": internal.DeviceListChanged,
"💣": internal.DeviceListChanged,
})
// Append even more items to New
err = table.Upsert(userID, deviceID, internal.MapStringInt{
"charlie": internal.DeviceListChanged, // we previously said "left" for charlie, so as "changed" is newer, we should see "changed"
"dave": internal.DeviceListLeft,
})
assertNoError(t, err)
// Now swap: all the combined items in New go into Sent
got, err = table.Select(userID, deviceID, true)
assertNoError(t, err)
assertVal(t, "swap select did not return combined new items", got, internal.MapStringInt{
"💣": internal.DeviceListChanged,
"charlie": internal.DeviceListChanged,
"dave": internal.DeviceListLeft,
})
// Swapping again clears Sent out, and since nothing is in New we get an empty list
got, err = table.Select(userID, deviceID, true)
assertNoError(t, err)
assertVal(t, "swap select did not return combined new items", got, internal.MapStringInt{})
// large updates work (chunker)
largeUpdate := internal.MapStringInt{}
for i := 0; i < 100000; i++ {
largeUpdate[fmt.Sprintf("user_%d", i)] = internal.DeviceListChanged
}
err = table.Upsert(userID, deviceID, largeUpdate)
assertNoError(t, err)
got, err = table.Select(userID, deviceID, true)
assertNoError(t, err)
assertVal(t, "swap select did not return large items", got, largeUpdate)
}