Skip to content

Commit

Permalink
Moved FakeWebsocket to a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhaleking committed Nov 25, 2024
1 parent 84829c0 commit 7850686
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
35 changes: 35 additions & 0 deletions tests/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from collections import deque
import json
from typing import Union

from websockets.sync.client import ClientConnection, ClientProtocol
from websockets.uri import parse_uri

from bittensor_wallet.mock.wallet_mock import MockWallet as _MockWallet
from bittensor_wallet.mock.wallet_mock import get_mock_coldkey
from bittensor_wallet.mock.wallet_mock import get_mock_hotkey
from bittensor_wallet.mock.wallet_mock import get_mock_wallet

from bittensor.utils.balance import Balance
from bittensor.core.chain_data import AxonInfo, NeuronInfo, PrometheusInfo
from tests.helpers.integration_websocket_data import WEBSOCKET_RESPONSES


def __mock_wallet_factory__(*_, **__) -> _MockWallet:
Expand Down Expand Up @@ -60,6 +66,7 @@ def get_mock_neuron(**kwargs) -> NeuronInfo:
"""

mock_neuron_d = dict(
# TODO fix the AxonInfo here — it doesn't work
{
"netuid": -1, # mock netuid
"axon_info": AxonInfo(
Expand Down Expand Up @@ -115,3 +122,31 @@ def get_mock_neuron_by_uid(uid: int, **kwargs) -> NeuronInfo:
return get_mock_neuron(
uid=uid, hotkey=get_mock_hotkey(uid), coldkey=get_mock_coldkey(uid), **kwargs
)


class FakeWebsocket(ClientConnection):
def __init__(self, *args, seed, **kwargs):
protocol = ClientProtocol(parse_uri("ws://127.0.0.1:9945"))
super().__init__(socket=None, protocol=protocol, **kwargs)
self.seed = seed
self.received = deque()

def send(self, payload: str, *args, **kwargs):
received = json.loads(payload)
id_ = received.pop("id")
self.received.append((received, id_))

def recv(self, *args, **kwargs):
item, _id = self.received.pop()
try:
response = WEBSOCKET_RESPONSES[self.seed][item["method"]][
json.dumps(item["params"])
]
response["id"] = _id
return json.dumps(response)
except (KeyError, TypeError):
print("ERROR", self.seed, item["method"], item["params"])
raise

def close(self, *args, **kwargs):
pass
38 changes: 1 addition & 37 deletions tests/integration_tests/test_subtensor_integration.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,10 @@
import json
from collections import deque
from websockets.sync.client import ClientConnection, ClientProtocol
from websockets.uri import parse_uri

import pytest
from bittensor.utils.balance import Balance
from bittensor.core.chain_data.axon_info import AxonInfo

from bittensor import NeuronInfo
from bittensor.core.subtensor import Subtensor
from tests.helpers.integration_websocket_data import WEBSOCKET_RESPONSES


class FakeWebsocket(ClientConnection):
def __init__(self, *args, seed, **kwargs):
protocol = ClientProtocol(parse_uri("ws://127.0.0.1:9945"))
super().__init__(socket=None, protocol=protocol, **kwargs)
self.seed = seed
self.received = deque()

def send(self, payload: str, *args, **kwargs):
received = json.loads(payload)
id_ = received.pop("id")
self.received.append((received, id_))

def recv(self):
item, _id = self.received.pop()
try:
response = WEBSOCKET_RESPONSES[self.seed][item["method"]][
json.dumps(item["params"])
]
response["id"] = _id
return json.dumps(response)
except KeyError:
print("ERROR", self.seed, item["method"], item["params"])
raise
except TypeError:
print("TypeError", response)
raise

def close(self, *args, **kwargs):
pass
from tests.helpers.helpers import FakeWebsocket


@pytest.fixture
Expand Down

0 comments on commit 7850686

Please sign in to comment.