Skip to content

Commit

Permalink
Merge branch 'main' into deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
callmephilip committed Nov 20, 2024
2 parents 86c5a65 + e3de260 commit e7eee8d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
8 changes: 6 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,12 @@ async def dispatch_incoming_message(m: ChannelMessage):
logger.debug(f"sockets {s}")
# send message to each socket
for c_s in s:
logger.debug(f"sending message to {c_s.sid} {connections[c_s.sid]}")
await connections[c_s.sid](Div(id=f"channel-{m.channel}", hx_swap="scroll:bottom", hx_swap_oob="afterbegin")(m_with_ctx))
conn = connections.get(c_s.sid)
if not conn: continue
logger.debug(f"sending message to {c_s.sid}")
try:
await conn(Div(id=f"channel-{m.channel}", hx_swap="scroll:bottom", hx_swap_oob="afterbegin")(m_with_ctx))
except: logger.error(f"Error sending message to {c_s.sid}")

@rt('/messages/send/{cid}', methods=['POST'])
async def send_msg(msg:str, cid:int, req: Request):
Expand Down
36 changes: 24 additions & 12 deletions locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# - https://docs.locust.io/en/stable/tasksets.html

from locust import HttpUser, task, between
import gevent, json
import gevent, json, re
from websockets.sync.client import connect

user_cnt: int = 0

class TinychatUser(HttpUser):
"""
This class represents simulated users interacting with
Expand All @@ -20,6 +22,15 @@ class TinychatUser(HttpUser):
# at any time
host = 'http://localhost:5001/'

def on_start(self):
global user_cnt
user_cnt += 1
self.client.post('/login', dict(name=f'locust-{user_cnt}'))
self.ws_connect()

def on_stop(self):
print(">>>>>>>>>>>>>>>>>>>>>>>> Stopping user >>>>>>>>>>>>>>>>>>>>>>>")

def ws_connect(self):
session_cookie = self.client.cookies.get_dict().get('session_')
self.ws = connect("ws://localhost:5001/ws?mid=3", additional_headers={"Cookie": f"session_={session_cookie}"})
Expand All @@ -28,24 +39,25 @@ def ws_connect(self):

def ws_receive_loop(self):
while True:
message = self.ws.recv()
print(f"WS Received: {message}")
self.ws.recv()
print(f"** WS Received data")

def ping_loop(self):
while True:
self.ws.send(json.dumps({"cmd": "ping", "d": {"cid": 1}}))
gevent.sleep(5)


@task
def chat(self):
# User goes to the root of the project
self.client.get('/')
self.client.get('/login')
self.client.post('/login', dict(name='locust'))
self.client.post('messages/send/1', dict(msg='Hello, world!'))
assert self.client.get('/c/1').status_code == 200

self.ws_connect()

r = self.client.get('/c/1')
assert r.status_code == 200
@task
def browse_chat_history(self):
url = "/c/messages/1"

for _ in range(5):
r = self.client.get(url, headers={"Hx-Request": "true"})
m = re.search(r'hx-get="/c/messages/\d+\?c=[A-Za-z0-9+/=-]+"', r.text)
if not m: return
url = m.group().split('hx-get=')[1].replace('"', '')

0 comments on commit e7eee8d

Please sign in to comment.