Skip to content

Commit

Permalink
Fix test error
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiddlet2666 committed Apr 11, 2024
1 parent 9f4557c commit df102ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/polyglot-build-jdk21.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..
Expand All @@ -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 ..
Expand All @@ -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 ..
8 changes: 6 additions & 2 deletions clients/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) => {
Expand All @@ -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',
Expand Down
65 changes: 47 additions & 18 deletions clients/py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -65,6 +60,11 @@ async def init_coherence():


async def run_demo() -> None:
"""
Run the command line demo.
:return: None
"""
global session

try:
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit df102ec

Please sign in to comment.