Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Depetrol committed Oct 11, 2024
1 parent 8c810de commit 4baee5b
Showing 1 changed file with 20 additions and 54 deletions.
74 changes: 20 additions & 54 deletions test/Python/src/concurrent/ConcurrentAction.lf
Original file line number Diff line number Diff line change
@@ -1,65 +1,31 @@
target Python {
coordination: decentralized # logging: debug
}

preamble {=
import subprocess
subprocess.run(["pip", "install", "uvicorn", "fastapi", "--user"], check=True)
=}
target Python

reactor WebServer(bank_index=0, STA=0) {
state app
reactor ConcurrentActionTest {
state logs
physical action addlog_action

reaction(startup) -> addlog_action {=
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import requests
print("Starting WebServer")
import time
import threading
self.logs = []
self.app = FastAPI()
self.app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@self.app.post("/log")
async def addlog(request: Request):
log_message = (await request.json())["log"]
print(f"Received log: {log_message}")
if log_message == "99":
print("Stopping FastAPI server")
os._exit(0)
addlog_action.schedule(0, log_message)
return {"status": "success"}
def test():
time.sleep(2)
def send_log(log_message, port=5000):
url = f"http://127.0.0.1:{port}/log"
log_data = {"log": f"{log_message}"}

def send_request():
try:
requests.post(url, json=log_data, timeout=100) # Send request without waiting for response
except Exception as e:
print(f"Error sending log: {e}")
def testall():
def test(i):
print(f"Scheduling action {i}")
addlog_action.schedule(0, f"{i}")

thread = threading.Thread(target=send_request)
thread.start()
for i in range(100):
send_log(f"{i}", port=5000)
def run_fastapi_app():
print(f"[WebServer{self.bank_index}] FastAPI server starting")
uvicorn.run(self.app, host="127.0.0.1", port=5000+self.bank_index, log_level="warning")
fastapi_thread = threading.Thread(target=run_fastapi_app)
fastapi_thread.start()
test_thread = threading.Thread(target=test)
test_thread.start()
threads = []
for i in range(100):
test_thread = threading.Thread(target=test, args=(i,))
test_thread.start()
threads.append(test_thread)
for thread in threads:
thread.join()
print("Test complete")
time.sleep(2)
os._exit(0)
testall_thread = threading.Thread(target=testall)
testall_thread.start()
=}

reaction(addlog_action) {=
Expand All @@ -73,5 +39,5 @@ reactor WebServer(bank_index=0, STA=0) {
}

main reactor {
server = new WebServer()
server = new ConcurrentActionTest()
}

0 comments on commit 4baee5b

Please sign in to comment.