Skip to content

Commit 94e4366

Browse files
authored
1.3.1 test container bump, added global timeout for requests (#49)
* Added possibility for global timeout for GRPC requests
1 parent a060704 commit 94e4366

File tree

8 files changed

+87
-8
lines changed

8 files changed

+87
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
make dev
2222
- name: Start immudb container
2323
run: |
24-
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v ${{ github.workspace }}/tests/certs/my.key.pem:/key.pem -p 3322:3322 codenotary/immudb:1.3.0 --signingKey=/key.pem
24+
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v ${{ github.workspace }}/tests/certs/my.key.pem:/key.pem -p 3322:3322 codenotary/immudb:1.3.1 --signingKey=/key.pem
2525
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v ${{ github.workspace }}/tests/certs/my.key.pem:/key.pem -p 3333:3322 codenotary/immudb:1.2.4 --signingKey=/key.pem
2626
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v ${{ github.workspace }}/tests/certs/my.key.pem:/key.pem -p 3344:3322 codenotary/immudb:1.1.0 --signingKey=/key.pem
2727
- name: Run tests

immudb/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434

3535
class ImmudbClient:
3636

37-
def __init__(self, immudUrl=None, rs: RootService = None, publicKeyFile: str = None):
37+
def __init__(self, immudUrl=None, rs: RootService = None, publicKeyFile: str = None, timeout=None):
3838
if immudUrl is None:
3939
immudUrl = "localhost:3322"
40+
self.timeout = timeout
4041
self.channel = grpc.insecure_channel(immudUrl)
4142
self._resetStub()
4243
if rs is None:
@@ -81,8 +82,9 @@ def set_token_header_interceptor(self, response):
8182
return self.get_intercepted_stub()
8283

8384
def get_intercepted_stub(self):
85+
allInterceptors = self.headersInterceptors + self.clientInterceptors
8486
intercepted, newStub = grpcutils.get_intercepted_stub(
85-
self.channel, self.headersInterceptors)
87+
self.channel, allInterceptors)
8688
self.intercept_channel = intercepted
8789
return newStub
8890

@@ -136,7 +138,12 @@ def logout(self):
136138

137139
def _resetStub(self):
138140
self.headersInterceptors = []
141+
self.clientInterceptors = []
142+
if(self.timeout != None):
143+
self.clientInterceptors.append(
144+
grpcutils.timeout_adder_interceptor(self.timeout))
139145
self.__stub = schema_pb2_grpc.ImmuServiceStub(self.channel)
146+
self.__stub = self.get_intercepted_stub()
140147

141148
def keepAlive(self):
142149
self.__stub.KeepAlive(google_dot_protobuf_dot_empty__pb2.Empty())

immudb/grpcutils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ class _ClientCallDetails(
2828
pass
2929

3030

31+
def timeout_adder_interceptor(stubTimeout=None):
32+
33+
def intercept_call(client_call_details, request_iterator, request_streaming,
34+
response_streaming):
35+
timeoutToSet = client_call_details.timeout
36+
if(timeoutToSet == None):
37+
timeoutToSet = stubTimeout
38+
client_call_details = _ClientCallDetails(
39+
client_call_details.method, timeoutToSet, client_call_details.metadata,
40+
client_call_details.credentials)
41+
return client_call_details, request_iterator, None
42+
43+
return generic_client_interceptor.create(intercept_call)
44+
45+
3146
def header_adder_interceptor(header, value):
3247

3348
def intercept_call(client_call_details, request_iterator, request_streaming,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
long_description = fh.read()
1818

1919
setup(name='immudb-py',
20-
version='1.3.0.post1',
20+
version='1.3.1',
2121
license="Apache License Version 2.0",
2222
description='Python SDK for Immudb',
2323
long_description=long_description,

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ def client_pem(request):
5353
def client(request):
5454
return client_margs(immudUrl=request.param)
5555

56-
5756
@pytest.fixture(scope="function", params=TESTURLS)
5857
def wrappedClient(request):
5958
return ImmuTestClient(client_margs(immudUrl=request.param))
59+
60+
@pytest.fixture(scope="function", params=TESTURLS)
61+
def argsToBuildClient(request):
62+
return (request.param, "immudb", "immudb")

tests/immu/test_massive_operation.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import string
1414
import random
1515
import time
16+
from tests.immuTestClient import ImmuTestClient
17+
import pytest
1618

1719

1820
def get_random_string(length):
@@ -41,5 +43,9 @@ def test_get_set_massive(self, client):
4143
assert i in xset
4244
assert xset[i] == resp[i]
4345

44-
def test_compact(self, client):
45-
client.compactIndex()
46+
def test_compact(self, wrappedClient: ImmuTestClient):
47+
# Snapshot compaction fixed from 1.3.1
48+
if(wrappedClient.serverHigherOrEqualsToVersion("1.3.0")):
49+
wrappedClient.client.compactIndex()
50+
else:
51+
pytest.skip("Server version too low")

tests/immu/test_timeout.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2022 CodeNotary, Inc. All rights reserved.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
from immudb.client import ImmudbClient
14+
import pytest
15+
from grpc import RpcError, StatusCode
16+
import uuid
17+
18+
def generateBigDict():
19+
toBuild = dict()
20+
keyList = []
21+
for _ in range(0, 128):
22+
bigStr = str(uuid.uuid4()) * 512
23+
key = str(uuid.uuid4()).encode("utf-8")
24+
keyList.append(key)
25+
toBuild[key] = bigStr.encode("utf-8")
26+
return toBuild, keyList
27+
28+
29+
class TestTimeout:
30+
def test_simple_timeout(self, argsToBuildClient):
31+
url, login, password = argsToBuildClient
32+
client = ImmudbClient(url, timeout=1)
33+
client.login(login, password)
34+
35+
with pytest.raises(RpcError) as excinfo:
36+
client = ImmudbClient(url, timeout=0.001)
37+
client.login(login, password)
38+
assert excinfo.value.code() == StatusCode.DEADLINE_EXCEEDED
39+
40+
41+
with pytest.raises(RpcError) as excinfo:
42+
client = ImmudbClient(url, timeout=0.05)
43+
client.login(login, password)
44+
keyVal, keys = generateBigDict()
45+
client.setAll(keyVal)
46+
scanned = client.scan(b"", b"", False, 400)
47+
assert excinfo.value.code() == StatusCode.DEADLINE_EXCEEDED
48+

tests/starttestcontainers.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
cd "$(dirname "$0")"
33

4-
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v "$PWD"/certs/my.key.pem:/key.pem -p 3322:3322 codenotary/immudb:1.3.0 --signingKey=/key.pem
4+
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v "$PWD"/certs/my.key.pem:/key.pem -p 3322:3322 codenotary/immudb:1.3.1 --signingKey=/key.pem
55
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v "$PWD"/certs/my.key.pem:/key.pem -p 3333:3322 codenotary/immudb:1.2.4 --signingKey=/key.pem
66
docker run -d --health-cmd "immuadmin status" --health-interval 10s --health-timeout 5s --health-retries 5 -v "$PWD"/certs/my.key.pem:/key.pem -p 3344:3322 codenotary/immudb:1.1.0 --signingKey=/key.pem

0 commit comments

Comments
 (0)