From df102ec8c3e06e976df64cb1d585fb04d0231423 Mon Sep 17 00:00:00 2001 From: Tim Middleton Date: Thu, 11 Apr 2024 12:31:27 +0800 Subject: [PATCH] Fix test error --- .github/workflows/polyglot-build-jdk21.yml | 6 +- clients/js/main.js | 8 ++- clients/py/main.py | 65 ++++++++++++++++------ 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/.github/workflows/polyglot-build-jdk21.yml b/.github/workflows/polyglot-build-jdk21.yml index 20918b8..c93df15 100644 --- a/.github/workflows/polyglot-build-jdk21.yml +++ b/.github/workflows/polyglot-build-jdk21.yml @@ -59,7 +59,7 @@ jobs: ./go-demo add-trades ORCL 1000 ./go-demo stock-split ORCL 2 ./go-demo monitor & - PID=$1 + PID=$! sleep 10 && kill -9 $PID cd .. @@ -72,7 +72,7 @@ jobs: node main.js add-trades DELL 1000 node main.js stock-split DELL 2 node main.js monitor & - PID=$1 + PID=$! sleep 10 && kill -9 $PID cd .. @@ -85,6 +85,6 @@ jobs: python3 main.py add-trades MSFT 1000 python3 main.py stock-split MSFT 2 python3 main.py monitor & - PID=$1 + PID=$! sleep 10 && kill -9 $PID cd .. \ No newline at end of file diff --git a/clients/js/main.js b/clients/js/main.js index 048b59d..531844d 100644 --- a/clients/js/main.js +++ b/clients/js/main.js @@ -29,6 +29,8 @@ const MapEventType = coh.event.MapEventType const session = new Session() const prices = session.getCache('Price') const trades = session.getCache('Trade') + +// currency formatter const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' @@ -69,9 +71,9 @@ setImmediate(async () => { process.exit(0) }) - // ----- helpers ------------------------------------------------------------ +// add a number of trades for a symbol async function addTrades(symbol, count) { if (count < 0) { console.log("Count must not be negative") @@ -108,6 +110,7 @@ async function addTrades(symbol, count) { console.log("Trades cache size is now " + size) } +// split a stock using a given factor async function stockSplit(symbol, factor) { if (factor < 0) { console.log("Factor must not be negative") @@ -145,7 +148,7 @@ async function stockSplit(symbol, factor) { await prices.invoke(symbol, Processors.multiply("price", 1 / factor)) } - +// monitor any price changes async function monitor() { console.log("Listening for price changes. Press CTRL-C to finish.") const handler = (event) => { @@ -171,6 +174,7 @@ function usage() { "stock-split - stock split, specify symbol and factor") } +// create a Trade function createTrade(symbol, qty, price) { const trade = { '@class': 'Trade', diff --git a/clients/py/main.py b/clients/py/main.py index 105c50b..d7d2d9d 100644 --- a/clients/py/main.py +++ b/clients/py/main.py @@ -15,19 +15,14 @@ # import uuid import random +import asyncio +import sys from typing import List from coherence import Filters, Aggregators, NamedCache, Session, Processors from coherence.event import MapListener from coherence import serialization -from uuid import uuid4 - -import asyncio -import sys - -sys.excepthook = lambda *args: None - @serialization.proxy("Price") class Price: @@ -36,10 +31,6 @@ def __init__(self, symbol: str, price: float): self.price = price -session: Session -prices: NamedCache[str, Price] - - @serialization.proxy("Trade") class Trade: def __init__(self, id: str, symbol: str, quantity: int, price: float): @@ -48,13 +39,17 @@ def __init__(self, id: str, symbol: str, quantity: int, price: float): self.quantity = quantity self.id = id - session: Session prices: NamedCache[str, Price] trades: NamedCache[str, Trade] -async def init_coherence(): +async def init_coherence() -> None: + """ + Initialized Coherence. + + :return: None + """ global session global prices global trades @@ -65,6 +60,11 @@ async def init_coherence(): async def run_demo() -> None: + """ + Run the command line demo. + + :return: None + """ global session try: @@ -95,7 +95,12 @@ async def run_demo() -> None: await session.close() -async def display_cache_size(): +async def display_cache_size() -> None: + """ + Displays the size for both the Trade and Price caches. + + :return: None + """ global prices global trades @@ -106,7 +111,12 @@ async def display_cache_size(): print(f"Price cache size: {pricesize}") -async def monitor_prices(): +async def monitor_prices() -> None: + """ + Monitors the Price cache for any changes and displays them. + + :return: None + """ global prices listener: MapListener[str, Price] = MapListener() @@ -117,7 +127,12 @@ async def monitor_prices(): await asyncio.sleep(10000) -def handle_event(e): +def handle_event(e) -> None: + """ + Event handler to display the event details + + :return: None + """ symbol = e.key old_price = e.old.price new_price = e.new.price @@ -127,7 +142,14 @@ def handle_event(e): f"Price changed for {symbol}, new=${new_price:.2f}, old=${old_price:.2f}, change=${change:.2f}") -async def add_trades(symbol: str, count: int): +async def add_trades(symbol: str, count: int) -> None: + """ + Add trades for a symbol. + + :param symbol the symbol to add trades to + :param count the number of trades to add + :return: None + """ global prices global trades @@ -162,7 +184,14 @@ async def add_trades(symbol: str, count: int): print(f"Unable to find {symbol}, valid symbols are {symbols}") -async def stock_split(symbol: str, factor: int): +async def stock_split(symbol: str, factor: int) -> None: + """ + Do a stock plit. + + :param symbol the symbol to split + :param factor the factor to use for the split, e.g. 2 = 2 to 1 + :return: None + """ global prices global trades