import socket
import urllib.request
import unittest
FLUVIO_ENDPOINT = "127.0.0.1:9003"
FLUVIO_HOST = "127.0.0.1"
FLUVIO_SC_PORT = 9003
def fluvio_available() -> bool:
try:
with socket.create_connection((FLUVIO_HOST, FLUVIO_SC_PORT), timeout=1):
return True
except OSError:
return False
FLUVIO_AVAILABLE = fluvio_available()
UNREACHABLE_ENDPOINT = "127.0.0.1:1"
class TestFluvioConstruction(unittest.TestCase):
def test_fluvio_sub_default_partition_and_offset(self):
from wingfoil import fluvio_sub
stream = fluvio_sub(UNREACHABLE_ENDPOINT, "topic")
self.assertIsNotNone(stream)
def test_fluvio_sub_with_partition_and_offset(self):
from wingfoil import fluvio_sub
stream = fluvio_sub(
UNREACHABLE_ENDPOINT, "topic", partition=2, start_offset=100
)
self.assertIsNotNone(stream)
def test_fluvio_pub_method_constructs_node(self):
from wingfoil import constant
node = constant({"value": b"hello"}).fluvio_pub(
UNREACHABLE_ENDPOINT, "topic"
)
self.assertIsNotNone(node)
def test_fluvio_pub_from_list_of_dicts_constructs_node(self):
from wingfoil import constant
records = [{"key": "k", "value": b"v"}, {"value": b"v2"}]
node = constant(records).fluvio_pub(UNREACHABLE_ENDPOINT, "topic")
self.assertIsNotNone(node)
class TestFluvioUnreachable(unittest.TestCase):
def test_fluvio_sub_negative_offset_errors(self):
from wingfoil import fluvio_sub
stream = fluvio_sub(
UNREACHABLE_ENDPOINT, "topic", partition=0, start_offset=-1
)
with self.assertRaises(Exception):
stream.collect().run(realtime=True, cycles=1)
def test_fluvio_pub_single_dict_marshals_then_errors(self):
from wingfoil import constant
node = constant({"value": b"hello", "key": "k"}).fluvio_pub(
UNREACHABLE_ENDPOINT, "topic"
)
with self.assertRaises(Exception):
node.run(realtime=True, cycles=1)
def test_fluvio_pub_list_of_dicts_marshals_then_errors(self):
from wingfoil import constant
records = [
{"key": "k1", "value": b"v1"},
{"value": b"v2"}, ]
node = constant(records).fluvio_pub(UNREACHABLE_ENDPOINT, "topic")
with self.assertRaises(Exception):
node.run(realtime=True, cycles=1)
def test_fluvio_pub_bad_value_type_marshals_empty_burst(self):
from wingfoil import constant
node = constant("not a dict").fluvio_pub(UNREACHABLE_ENDPOINT, "topic")
with self.assertRaises(Exception):
node.run(realtime=True, cycles=1)
@unittest.skipUnless(FLUVIO_AVAILABLE, f"Fluvio not running on {FLUVIO_ENDPOINT}")
class TestFluvioSub(unittest.TestCase):
def test_sub_returns_list(self):
from wingfoil import fluvio_sub
stream = fluvio_sub(FLUVIO_ENDPOINT, "test-python-sub", partition=0).collect()
stream.run(realtime=True, cycles=0)
def test_sub_event_dict_shape(self):
from wingfoil import fluvio_sub
stream = (
fluvio_sub(FLUVIO_ENDPOINT, "test-python-sub", partition=0)
.collect()
)
stream.run(realtime=True, cycles=1)
result = stream.peek_value()
self.assertIsInstance(result, list)
if result: event = result[0]
self.assertIsInstance(event, dict)
self.assertIn("key", event)
self.assertIn("value", event)
self.assertIn("offset", event)
self.assertIsInstance(event["value"], bytes)
self.assertIsInstance(event["offset"], int)
@unittest.skipUnless(FLUVIO_AVAILABLE, f"Fluvio not running on {FLUVIO_ENDPOINT}")
class TestFluvioPub(unittest.TestCase):
def test_pub_single_dict(self):
from wingfoil import constant
constant({"value": b"hello from python"}).fluvio_pub(
FLUVIO_ENDPOINT, "test-python-pub"
).run(realtime=True, cycles=1)
def test_pub_keyed_dict(self):
from wingfoil import constant
constant({"key": "my-key", "value": b"keyed value"}).fluvio_pub(
FLUVIO_ENDPOINT, "test-python-pub"
).run(realtime=True, cycles=1)
def test_pub_list_of_dicts(self):
from wingfoil import constant
records = [
{"key": "k1", "value": b"first"},
{"key": "k2", "value": b"second"},
]
constant(records).fluvio_pub(
FLUVIO_ENDPOINT, "test-python-pub"
).run(realtime=True, cycles=1)
if __name__ == "__main__":
unittest.main()