Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Commit

Permalink
Fixes issue wtolson#25 - publish error with TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
herberthamaral authored and bgeisberger committed Nov 12, 2020
1 parent 1303230 commit b36d3dc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ venv.bak/

# mypy
.mypy_cache/

# vim
*.sw*
5 changes: 3 additions & 2 deletions gnsq/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ def handle_response(self, conn, response):
self.logger.debug('[%s] response: %s', conn, response)

if response == nsq.OK:
result = self._response_queues[conn].popleft()
result.set(response)
if conn in self._response_queues:
result = self._response_queues[conn].popleft()
result.set(response)

self.on_response.send(self, response=response)

Expand Down
8 changes: 5 additions & 3 deletions tests/integration_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BaseIntegrationServer(object):
protocol_re = re.compile(' '.join([
r'(?P<protocol>[A-Z]+):',
r'listening on',
r'(?P<address>(?:[0-9]{1,3}\.){3}[0-9]{1,3}):(?P<port>[0-9]+)',
r'(?P<address>((?:[0-9]{1,3}\.){3}[0-9]{1,3}|(\[\:\:\]))):(?P<port>[0-9]+)',
]))

version_re = re.compile(' '.join([
Expand Down Expand Up @@ -116,13 +116,14 @@ class NsqdIntegrationServer(BaseIntegrationServer):
tls_cert = os.path.join(os.path.dirname(__file__), 'cert.pem')
tls_key = os.path.join(os.path.dirname(__file__), 'key.pem')

def __init__(self, lookupd=None, **kwargs):
def __init__(self, lookupd=None, extra_params=[], **kwargs):
super(NsqdIntegrationServer, self).__init__(**kwargs)

if self.has_https():
self.protocols = ('TCP', 'HTTP', 'HTTPS')

self.lookupd = lookupd
self.extra_params = extra_params

def has_https(self):
return self.version >= (0, 2, 28)
Expand All @@ -146,8 +147,9 @@ def cmd(self):
'--tls-cert', self.tls_cert,
'--tls-key', self.tls_key,
]
cmd.extend(self.extra_params)

if self.has_https():
if self.has_https() and '--https-address' not in cmd:
cmd.extend(['--https-address', self._random_address()])

if self.lookupd:
Expand Down
36 changes: 36 additions & 0 deletions tests/test_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import gevent
import urllib3

from gnsq import NsqdHTTPClient, Producer
from gnsq.errors import NSQException, NSQNoConnections, NSQInvalid
Expand Down Expand Up @@ -110,6 +111,41 @@ def test_publish_error():
producer.join()


@pytest.mark.slow
@pytest.mark.timeout(SLOW_TIMEOUT)
def test_tls_publish():
extra_params = [
'--tls-required', 'true',
'--https-address', '127.0.0.1:4152',
]
with NsqdIntegrationServer(extra_params=extra_params) as server:
producer = Producer(
server.tcp_address,
tls_options={
'keyfile': server.tls_key,
'certfile': server.tls_cert,
},
tls_v1=True,
)
producer.start()

for _ in range(100):
producer.publish('test', b'hi')

producer.close()
producer.join()

conn = NsqdHTTPClient(
server.address,
'4152',
connection_class=urllib3.HTTPSConnectionPool,
cert_reqs='CERT_NONE',
)
stats = conn.stats()

assert stats['topics'][0]['depth'] == 100


def test_not_running():
producer = Producer('192.0.2.1:4150')

Expand Down

0 comments on commit b36d3dc

Please sign in to comment.