async def reduce_finish(self, action: redux.Action, changed_state: Dict[str, Any]):
     if action == "INCREASE_EQUITY":
         equity = self.get_state()["equity"]
         action = redux.Action("EQUITY_CHANGED", equity=equity)
         await self.send(action, redux.LocalMedium(self.store), UserService, self.node_id)
     if action == "NOT_ENOUGH_EQUITY":
         key = f"entry:user:{self.node_id}"
         action = redux.Action("NOT_ENOUGH_EQUITY")
         await self.send(action, redux.LocalMedium(self.store), UserService, self.node_id)
async def work():
    store = redux.Store([CombineReducer])
    await store.dispatch("combine:1", redux.Action("START"))
    await asyncio.sleep(0.2)
    assert timeout_future.done()
    await store.dispatch("combine:1", redux.Action("START"))
    await store.dispatch("combine:1", redux.Action("A"))
    await store.dispatch("combine:1", redux.Action("B"))
    await asyncio.sleep(0.2)
    assert combine_future.done()
Exemple #3
0
async def fetch_state():
    redux.RemoteManager().client_url.add("ws://127.0.0.1:9906")
    store = redux.Store([ReducerStateProvider, ReducerStateFetcher])
    server_opt = await redux.RemoteManager().serve("127.0.0.1", 9906, store)
    await asyncio.sleep(0.11)
    server = server_opt.unwrap()
    await store.dispatch("user", redux.Action("todo"))
    await store.dispatch("fetcher", redux.Action("todo"))
    asyncio.wait_for(refetch_future, 1)
    await redux.RemoteManager().stop_serve(server)
 async def action_received(self, action: redux.Action):
     if action == "START":
         self.combine_message(["A", "B"], redux.Action("COMBINE_FINISH"),
                              redux.Action("COMBINE_ERROR"), 0.1)
     elif action == "COMBINE_ERROR":
         timeout_future.set_result(True)
     elif action == "COMBINE_FINISH":
         combine_future.set_result(True)
     else:
         print(action)
Exemple #5
0
 async def action_received(self, action: redux.Action):
     if action == "VERIFY_LOGIN":
         user_name = action.arguments["user_name"]
         password = action.arguments["password"]
         if user_name == "kenny" and password == "123456":
             if action.medium:
                 resp_action = redux.Action("ACCESS_LOGIN", user_name=user_name)
                 await self.response(resp_action, action.medium, action.source_key)
         else:
             if action.medium:
                 resp_action = redux.Action("DENIED_LOGIN", user_name=user_name)
                 await self.response(resp_action, action.medium, action.source_key, )
 async def action_received(self, action: redux.Action):
     if action == "TRANSFER":
         from_user = action.arguments["from"]
         to_user = action.arguments["to"]
         change = action.arguments["change"]
         await self.send(redux.Action.no_op_command(), redux.LocalMedium(self.store), UserNode, from_user)
         result = await self.modify_database(from_user, to_user, change)
         medium = redux.LocalMedium(self.store)
         if result:
             await self.send(redux.Action("INCREASE_EQUITY", change=-change), medium, UserNode, from_user)
             await self.send(redux.Action("INCREASE_EQUITY", soft=True, change=change), medium, UserNode, to_user)
         else:
             await self.send(redux.Action("NOT_ENOUGH_EQUITY"), medium, UserNode, from_user)
Exemple #7
0
 async def reduce_finish(self, action: redux.Action,
                         changed_state: Dict[str, Any]):
     if self.key == "1" and action.type == "todo":
         target = await redux.RemoteMedium.connect(self.store,
                                                   "ws://127.0.0.1:9906")
         target = target.unwrap()
         await self.send(target, "2", redux.Action("hello", time=4321))
     if self.key == "1" and action.type == "hello":
         assert action.arguments == dict(time=1)
         socket_sent_future.set_result(True)
     if self.key == "2":
         await self.send(action.medium, action.source_key,
                         redux.Action("hello", time=1))
Exemple #8
0
 async def reduce_finish(self, action: redux.Action,
                         changed_state: Dict[str, Any]):
     if self.key == "1":
         assert action.medium is None
         assert action.source_key is None
         opt = await self.send(action.medium, action.source_key,
                               redux.Action("hello", time=1234))
         assert opt.is_error
         local_source = await redux.LocalMedium.connect(self.store)
         await self.send(local_source, "2", redux.Action("hello",
                                                         time=4321))
     if self.key == "2":
         assert isinstance(action.medium, redux.LocalMedium)
         assert action.source_key == "1"
         assert action.arguments == dict(time=4321)
Exemple #9
0
def test_action():
    action = redux.Action("one")
    assert action == "one"
    assert action != "two"
    assert action.type == "one"
    assert action.type == action.type
    assert action in ["three", "two", "one"]
Exemple #10
0
async def send_in_socket():
    redux.RemoteManager().client_url.add("ws://127.0.0.1:9906")
    store = redux.Store([ReducerWithSocketSend])
    server_opt = await redux.RemoteManager().serve("127.0.0.1", 9906, store)
    server = server_opt.unwrap()
    await store.dispatch("1", redux.Action("todo"))
    asyncio.wait_for(socket_sent_future, 1)
    await redux.RemoteManager().stop_serve(server)
Exemple #11
0
 async def action_received(self, action: redux.Action):
     if isinstance(action.medium, redux.EntryMedium):
         if action == "LOGIN":
             user_name = action.arguments["userName"]
             password = action.arguments["password"]
             action = redux.Action("VERIFY_LOGIN", user_name=user_name, password=password)
             await self.send(action, redux.LocalMedium(self.store), LoginVerify, user_name)
     if isinstance(action.medium, redux.LocalMedium):
         if action == "ACCESS_LOGIN":
             print(f"ACCESS_LOGIN {action.arguments['user_name']}")
         if action == "DENIED_LOGIN":
             print(f"DENIED_LOGIN {action.arguments['user_name']}")
Exemple #12
0
    async def action_received(self, action: redux.Action):
        target = await redux.RemoteMedium.connect(self.store,
                                                  "ws://127.0.0.1:9906")
        state = await self.get_remote_state(target.unwrap(), "user")
        assert state.unwrap() == dict(name="provider", age=1)
        state = await self.get_remote_state(redux.LocalMedium(self.store),
                                            "user")
        assert state.unwrap() == dict(name="provider", age=1)

        state = await self.get_remote_state(target.unwrap(), "user", ["name"])
        assert state.unwrap() == dict(name="provider")
        state = await self.get_remote_state(redux.LocalMedium(self.store),
                                            "user", ["age"])
        assert state.unwrap() == dict(age=1)

        await self.send(target.unwrap(), "user", redux.Action("CALL_ME"))
Exemple #13
0
async def basic_redux():
    store = redux.Store([ReducerBasic])
    await store.dispatch("1", redux.Action("todo"))
    handler = (await store.subscribe("1", L())).unwrap()
    await store.dispatch("1", redux.Action("NAME", name="peter"))
    handler()
    await store.dispatch("1", redux.Action("NAME", name="john"))
    handler = (await store.subscribe("1", L())).unwrap()
    await store.dispatch("1", redux.Action("NAME", name="bob"))
    await store.dispatch("1", redux.Action("NAME", name="jim"))
    await store.dispatch("1", redux.Action("NAME", name="tony"))
Exemple #14
0
 async def action_received(self, action: redux.Action):
     medium = self.entry_medium
     time = str(datetime.now())
     await self.send(redux.Action("TIME", time=time, name=self.node_id), medium)
Exemple #15
0
async def local_subscribe():
    store = redux.Store(
        [LocalMediumSubscribeReducer, LocalMediumListenerReducer])
    await store.dispatch("listener:1", redux.Action("sub"))
    await store.dispatch("sub:local:1", redux.Action("setName"))
    await asyncio.sleep(5)
Exemple #16
0
async def send_in_process():
    store = redux.Store([ReducerWithSend])
    await store.dispatch("1", redux.Action("todo"))