mindflayer / python-mocket

a socket mock framework - for all kinds of socket animals, web-clients included
BSD 3-Clause "New" or "Revised" License
280 stars 42 forks source link

[mocket-3.10.x] AttributeError: type object 'Mocket' has no attribute '_address' #191

Closed amotl closed 1 year ago

amotl commented 1 year ago

Dear Giorgio,

while giving some love to Terkin, and creating https://github.com/hiveeyes/terkin-datalogger/pull/116, I also wanted to report about another issue which blocked me from upgrading to mocket-3.10.x already.

>>> import mocket
>>> mocket.__version__
'3.10.8'
pytest test/test_lorawan.py -k test_uplink_system_temperature --no-cov
ERROR    terkin.telemetry.core:core.py:501 [LoRa] Transmission failed
Traceback (most recent call last):
  File "/Users/amo/dev/hiveeyes/sources/terkin-datalogger/src/lib/terkin/telemetry/core.py", line 496, in send
    outcome = self.lora_adapter.send(payload)
  File "/Users/amo/dev/hiveeyes/sources/terkin-datalogger/src/lib/terkin/network/lora.py", line 36, in send
    return self.driver.send(payload)
  File "/Users/amo/dev/hiveeyes/sources/terkin-datalogger/src/lib/terkin/network/lora.py", line 177, in send
    success = self.socket.send(payload)
  File "/Users/amo/dev/hiveeyes/sources/terkin-datalogger/.venv310/lib/python3.10/site-packages/mocket/mocket.py", line 399, in send
    entry = self.get_entry(data)
  File "/Users/amo/dev/hiveeyes/sources/terkin-datalogger/.venv310/lib/python3.10/site-packages/mocket/mocket.py", line 266, in get_entry
    return Mocket.get_entry(self._host, self._port, data)
  File "/Users/amo/dev/hiveeyes/sources/terkin-datalogger/.venv310/lib/python3.10/site-packages/mocket/mocket.py", line 438, in get_entry
    host = host or Mocket._address[0]
AttributeError: type object 'Mocket' has no attribute '_address'

It looks like the _address attribute moved to MocketSocket, so it can't be addressed on the Mocket instance anymore. Is there anything you could do about it?

With kind regards, Andreas.

mindflayer commented 1 year ago

Hi @amotl, I had a look at your project and I noticed the way you are using mocket is very uncommon. You don't seem to be using its decorator nor its context manager, which is probably the reason why Mocket does not have that attribute set properly (connect is not happening inside Mocket's scope). Could you please help me with a snippet of code that fail the same way as your tests? The _address attribute wasn't moved, this is the only recent change related to it: https://github.com/mindflayer/python-mocket/pull/183/files#diff-bdbad69094211a1bec191da22004fa4eea8ed1e953a16e52da1e442036f18b47R430

mindflayer commented 1 year ago

I even see you are creating a MocketEntry with (None, None) as location, which makes no sense because no client would try to connect to that couple of non-existing host/port.

amotl commented 1 year ago

Dear Giorgio,

thanks a stack for taking the time to look into this, and for discovering our irregular use of Mocket. I have to admit I did not look too closely when "just bumping some development dependencies" ;].

So, it appears that we will have to do our homework and review/modify our usage of Mocket accordingly. On that matter, I want to avoid bothering you any longer about it, and diverted this issue back to https://github.com/hiveeyes/terkin-datalogger/issues/117 again.

I even see you are creating a MocketEntry with (None, None) as location, which makes no sense because no client would try to connect to that couple of non-existing host/port.

I think that was needed to support the irregular socket.AF_LORA socket type, which completely lacks the host/port addressing scheme.

Thank you again and with kind regards, Andreas.

mindflayer commented 1 year ago

Nevertheless, @amotl let me know if you need any kind of support on my end, no matter if it's for something Mocket supports at the moment or not.

amotl commented 1 year ago

Dear Giorgio,

your offer is very kind. However, I don't have any other specific need for help other than offering a detailed reproduction, if you are really keen enough that you would like to look directly into the issue on behalf of the code base where I have been observing it.

If you can quickly spot what I was doing wrong, and see an immediate solution, I will be more than happy. Otherwise, please don't waste your valuable time on this matter. The workable solution is to just downgrade to mocket-3.9.x, so it is currently not a blocker for the evolution of Terkin - however it may well become in the future.

With kind regards, Andreas.

Success

# Acquire sources and create sandbox environment.
git clone https://github.com/hiveeyes/terkin-datalogger
cd terkin-datalogger/
python3.10 -m venv .venv310
source .venv310/bin/activate

# Install dependency packages from PyPI.
pip install --editable=.[lorawan] --requirement=requirements-test.txt

# Install more requirements manually. We will have to improve this.
wget https://raw.githubusercontent.com/daq-tools/micropython/improve-onewire/drivers/onewire/onewire.py --output-document=dist-packages/onewire_native.py
wget https://raw.githubusercontent.com/micropython/micropython/v1.11/drivers/onewire/ds18x20.py --output-document=dist-packages/ds18x20_native.py
wget https://raw.githubusercontent.com/robert-hh/BME280/79ccf348ec674f15c92a1debf1aceb383db38321/bme280_float.py --output-document=dist-packages/bme280_float.py 

# Run specific set of software tests, to save cycles.
pytest test/test_lorawan.py

Error

See it failing after upgrading to most recent version of mocket.

pip install mocket --upgrade
pytest --no-cov test/test_lorawan.py -k test_uplink_system_temperature

Works again

Make it work again by downgrading to mocket-3.9.x.

pip install "mocket<3.10" --upgrade
pytest --no-cov test/test_lorawan.py -k test_uplink_system_temperature
mindflayer commented 1 year ago

I had a look again at what you did. It's definitely something I have never seen before, but it works, and the fix was super-easy.

Here is a new Mocket version for you: https://pypi.org/project/mocket/3.10.9/

amotl commented 1 year ago

Hi again,

after a bit more fiddling at https://github.com/hiveeyes/terkin-datalogger/pull/116, it now works like a charm. Thank you so much for the exceptional quick fix and release!

With kind regards, Andreas.