|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/matrix-org/complement" |
| 10 | + "github.com/matrix-org/complement/client" |
| 11 | + "github.com/matrix-org/complement/helpers" |
| 12 | + "github.com/tidwall/gjson" |
| 13 | +) |
| 14 | + |
| 15 | +// Test that to-device messages can go from one homeserver to another. |
| 16 | +func TestToDeviceMessagesOverFederation(t *testing.T) { |
| 17 | + deployment := complement.Deploy(t, 2) |
| 18 | + defer deployment.Destroy(t) |
| 19 | + |
| 20 | + testCases := []struct { |
| 21 | + name string |
| 22 | + makeUnreachable func(t *testing.T) |
| 23 | + makeReachable func(t *testing.T) |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "good connectivity", |
| 27 | + makeUnreachable: func(t *testing.T) {}, |
| 28 | + makeReachable: func(t *testing.T) {}, |
| 29 | + }, |
| 30 | + { |
| 31 | + // cut networking but keep in-memory state |
| 32 | + name: "interrupted connectivity", |
| 33 | + makeUnreachable: func(t *testing.T) { |
| 34 | + deployment.StopServer(t, "hs2") |
| 35 | + }, |
| 36 | + makeReachable: func(t *testing.T) { |
| 37 | + deployment.StartServer(t, "hs2") |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + // interesting because this nukes memory |
| 42 | + name: "stopped server", |
| 43 | + makeUnreachable: func(t *testing.T) { |
| 44 | + deployment.StopServer(t, "hs2") |
| 45 | + }, |
| 46 | + makeReachable: func(t *testing.T) { |
| 47 | + // kick over the sending server first to see if the server |
| 48 | + // remembers to resend on startup |
| 49 | + deployment.StopServer(t, "hs1") |
| 50 | + deployment.StartServer(t, "hs1") |
| 51 | + // now make the receiving server reachable. |
| 52 | + deployment.StartServer(t, "hs2") |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tc := range testCases { |
| 58 | + tc := tc |
| 59 | + t.Run(tc.name, func(t *testing.T) { |
| 60 | + alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{ |
| 61 | + LocalpartSuffix: "alice", |
| 62 | + }) |
| 63 | + bob := deployment.Register(t, "hs2", helpers.RegistrationOpts{ |
| 64 | + LocalpartSuffix: "bob", |
| 65 | + }) |
| 66 | + // it might take a while for retries, so keep on syncing! |
| 67 | + bob.SyncUntilTimeout = 30 * time.Second |
| 68 | + |
| 69 | + _, bobSince := bob.MustSync(t, client.SyncReq{TimeoutMillis: "0"}) |
| 70 | + |
| 71 | + content := map[string]interface{}{ |
| 72 | + "my_key": "my_value", |
| 73 | + } |
| 74 | + |
| 75 | + tc.makeUnreachable(t) |
| 76 | + |
| 77 | + alice.MustSendToDeviceMessages(t, "my.test.type", map[string]map[string]map[string]interface{}{ |
| 78 | + bob.UserID: { |
| 79 | + bob.DeviceID: content, |
| 80 | + }, |
| 81 | + }) |
| 82 | + |
| 83 | + checkEvent := func(result gjson.Result) bool { |
| 84 | + if result.Get("type").Str != "my.test.type" { |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + evContentRes := result.Get("content") |
| 89 | + |
| 90 | + if !evContentRes.Exists() || !evContentRes.IsObject() { |
| 91 | + return false |
| 92 | + } |
| 93 | + |
| 94 | + evContent := evContentRes.Value() |
| 95 | + |
| 96 | + return reflect.DeepEqual(evContent, content) |
| 97 | + } |
| 98 | + // just in case the server returns 200 OK before flushing to disk, give it a grace period. |
| 99 | + // This is too nice of us given in the real world no grace is provided.. |
| 100 | + time.Sleep(time.Second) |
| 101 | + |
| 102 | + tc.makeReachable(t) |
| 103 | + |
| 104 | + // servers may need to be poked with another to-device msg. This isn't great. |
| 105 | + // See https://github.com/matrix-org/synapse/issues/16680 |
| 106 | + // bob has a sync timeout of 30s set, so if the test has not yet passed, we are kicking the server |
| 107 | + // after 10s to ensure the server processes the previous sent to-device message. |
| 108 | + var completed atomic.Bool |
| 109 | + go func() { |
| 110 | + time.Sleep(10 * time.Second) |
| 111 | + if completed.Load() { |
| 112 | + return |
| 113 | + } |
| 114 | + // maybe kicking the server will make things work if we're still waiting after 10s |
| 115 | + alice.MustSendToDeviceMessages(t, "kick.type", map[string]map[string]map[string]interface{}{ |
| 116 | + bob.UserID: { |
| 117 | + bob.DeviceID: content, |
| 118 | + }, |
| 119 | + }) |
| 120 | + }() |
| 121 | + |
| 122 | + bob.MustSyncUntil(t, client.SyncReq{Since: bobSince}, func(clientUserID string, topLevelSyncJSON gjson.Result) error { |
| 123 | + t.Logf("%s", topLevelSyncJSON.Raw) |
| 124 | + return client.SyncToDeviceHas(alice.UserID, checkEvent)(clientUserID, topLevelSyncJSON) |
| 125 | + }) |
| 126 | + completed.Store(true) |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
0 commit comments