-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathremote_tarantool_server.py
More file actions
128 lines (99 loc) · 3.28 KB
/
Copy pathremote_tarantool_server.py
File metadata and controls
128 lines (99 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
This module provides helpers to work with remote Tarantool server
(used on Windows).
"""
import sys
import os
import random
import string
import time
from .tarantool_admin import TarantoolAdmin
# a time during which try to acquire a lock
AWAIT_TIME = 60 # seconds
# on which port bind a socket for binary protocol
BINARY_PORT = 3301
def get_random_string():
"""
:type: :obj:`str`
"""
return ''.join(random.choice(string.ascii_lowercase) for _ in range(16))
class RemoteTarantoolServer():
"""
Class to work with remote Tarantool server.
"""
def __init__(self):
self.host = os.environ['REMOTE_TARANTOOL_HOST']
self.args = {}
self.args['primary'] = BINARY_PORT
self.args['admin'] = os.environ['REMOTE_TARANTOOL_CONSOLE_PORT']
assert self.args['primary'] != self.args['admin']
# a name to using for a lock
self.whoami = get_random_string()
self.admin = TarantoolAdmin(self.host, self.args['admin'])
self.lock_is_acquired = False
# emulate stopped server
self.acquire_lock()
self.admin.execute('box.cfg{listen = box.NULL}')
def acquire_lock(self):
"""
Acquire lock on the remote server so no concurrent tests would run.
"""
deadline = time.time() + AWAIT_TIME
while True:
res = self.admin.execute(f'return acquire_lock("{self.whoami}")')
ok = res[0]
err = res[1] if not ok else None
if ok:
break
if time.time() > deadline:
raise RuntimeError(f'can not acquire "{self.whoami}" lock: {str(err)}')
print(f'waiting to acquire "{self.whoami}" lock',
file=sys.stderr)
time.sleep(1)
self.lock_is_acquired = True
def touch_lock(self):
"""
Refresh locked state on the remote server so no concurrent
tests would run.
"""
assert self.lock_is_acquired
res = self.admin.execute(f'return touch_lock("{self.whoami}")')
ok = res[0]
err = res[1] if not ok else None
if not ok:
raise RuntimeError(f'can not update "{self.whoami}" lock: {str(err)}')
def release_lock(self):
"""
Release loack so another test suite can run on the remote
server.
"""
res = self.admin.execute(f'return release_lock("{self.whoami}")')
ok = res[0]
err = res[1] if not ok else None
if not ok:
raise RuntimeError(f'can not release "{self.whoami}" lock: {str(err)}')
self.lock_is_acquired = False
def start(self):
"""
Initialize the work with the remote server.
"""
if not self.lock_is_acquired:
self.acquire_lock()
self.admin.execute(f'box.cfg{{listen = "0.0.0.0:{self.args["primary"]}"}}')
def stop(self):
"""
Finish the work with the remote server.
"""
self.admin.execute('box.cfg{listen = box.NULL}')
self.release_lock()
def is_started(self):
"""
Check if we still work with the remote server.
"""
return self.lock_is_acquired
def clean(self):
"""
Do nothing.
"""
def __del__(self):
self.admin.disconnect()