git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/tests/matrix-appservice-irc: Fix test

Migrate from the python matrix SDK to Matrix NIO

commit4bf7da5720c6de80251f2dbef13eac79a0fe4959 authorpiegames <git@piegames.de> dateWed 29 Sep 2021, 11:41 (5 years ago) parentf34e810
1 file changed+54−40
nixos/tests/matrix-appservice-irc.nix+54−40
nixos/tests/matrix-appservice-irc.nix+54−40
@@ -25,7 +25,7 @@ import ./make-test-python.nix ({ pkgs, ... }:
2525 "bind_address" = "";
2626 "port" = 8448;
2727 "resources" = [
28− { "compress" = true; "names" = [ "client" "webclient" ]; }
28+ { "compress" = true; "names" = [ "client" ]; }
2929 { "compress" = false; "names" = [ "federation" ]; }
3030 ];
3131 "tls" = false;
@@ -85,45 +85,60 @@ import ./make-test-python.nix ({ pkgs, ... }:
8585 client = { pkgs, ... }: {
8686 environment.systemPackages = [
8787 (pkgs.writers.writePython3Bin "do_test"
88− { libraries = [ pkgs.python3Packages.matrix-client ]; } ''
88+ { libraries = [ pkgs.python3Packages.matrix-nio ]; } ''
8989 import socket
90− from matrix_client.client import MatrixClient
90+ from nio import AsyncClient, RoomMessageText
9191 from time import sleep
92−
93− matrix = MatrixClient("${homeserverUrl}")
94− matrix.register_with_password(username="alice", password="foobar")
95−
96− irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
97− irc.connect(("ircd", 6667))
98− irc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
99− irc.send(b"USER bob bob bob :bob\n")
100− irc.send(b"NICK bob\n")
101−
102− m_room = matrix.join_room("#irc_#test:homeserver")
103− irc.send(b"JOIN #test\n")
104−
105− # plenty of time for the joins to happen
106− sleep(10)
107−
108− m_room.send_text("hi from matrix")
109− irc.send(b"PRIVMSG #test :hi from irc \r\n")
110−
111− print("Waiting for irc message...")
112− while True:
113− buf = irc.recv(10000)
114− if b"hi from matrix" in buf:
115− break
116−
117− print("Waiting for matrix message...")
118−
119−
120− def callback(room, e):
121− if "hi from irc" in e['content']['body']:
122− exit(0)
123−
124−
125− m_room.add_listener(callback, "m.room.message")
126− matrix.listen_forever()
92+ import asyncio
93+
94+
95+ async def run():
96+ matrix = AsyncClient("${homeserverUrl}")
97+ await matrix.register("alice", "foobar")
98+
99+ irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
100+ irc.connect(("ircd", 6667))
101+ irc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
102+ irc.send(b"USER bob bob bob :bob\n")
103+ irc.send(b"NICK bob\n")
104+
105+ room_id = (await matrix.join("#irc_#test:homeserver")).room_id
106+ irc.send(b"JOIN #test\n")
107+
108+ # plenty of time for the joins to happen
109+ sleep(10)
110+
111+ # Exchange messages
112+ await matrix.room_send(
113+ room_id=room_id,
114+ message_type="m.room.message",
115+ content={
116+ "msgtype": "m.text",
117+ "body": "hi from matrix"
118+ }
119+ )
120+ irc.send(b"PRIVMSG #test :hi from irc \r\n")
121+
122+ print("Waiting for irc message...")
123+ while True:
124+ buf = irc.recv(10000)
125+ if b"hi from matrix" in buf:
126+ print("Got IRC message")
127+ break
128+
129+ print("Waiting for matrix message...")
130+
131+ async def callback(room, e):
132+ if "hi from irc" in e.body:
133+ print("Got Matrix message")
134+ await matrix.close()
135+ exit(0) # Actual exit point
136+
137+ matrix.add_event_callback(callback, RoomMessageText)
138+ await matrix.sync_forever()
139+ exit(1) # Unreachable
140+
141+ asyncio.run(run())
127142 ''
128143 )
129144 ];
@@ -158,7 +173,6 @@ import ./make-test-python.nix ({ pkgs, ... }:
158173 homeserver.wait_for_open_port(8448)
159174
160175 with subtest("ensure messages can be exchanged"):
161− client.succeed("do_test")
176+ client.succeed("do_test >&2")
162177 '';
163−
164178 })