generated from opentensor/bittensor-subnet-template
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_store.py
370 lines (307 loc) · 9.85 KB
/
test_store.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import os
import time
import shutil
import pytest
import sqlite3
import pandas as pd
import asyncio
from pathlib import Path
from folding.store import SQLiteJobStore, MockJob, Job
ROOT_PATH = Path(__file__).parent
DB_PATH = os.path.join(ROOT_PATH, "mock_data")
PDB = "ab12"
FF = "charmm27"
BOX = "cubic"
WATER = "tip3p"
@pytest.fixture(autouse=True)
def cleanup():
yield
if os.path.exists(DB_PATH):
shutil.rmtree(DB_PATH)
@pytest.mark.parametrize("mock", [True, False])
def test_create_job(mock):
if mock:
MockJob()
else:
Job(
pdb=PDB,
ff=FF,
box=BOX,
water=WATER,
hotkeys=["a", "b", "c", "d"],
created_at=time.time(),
updated_at=time.time(),
)
@pytest.mark.parametrize(
"loss, commit_hash, gro_hash",
[
(-0.1, "1234", "5678"),
(-0.2, "1234", "5678"),
(-0.3, "1234", "5678"),
(-0.4, "1234", "5678"),
],
)
@pytest.mark.asyncio()
async def test_update_job(loss, commit_hash, gro_hash):
job = MockJob()
prev_loss = job.best_loss
hotkey = job.hotkeys[0]
await job.update(loss=loss, hotkey=hotkey)
assert (
job.updated_count == 1
), f"updated count should be 1, currently is {job.updated_count}"
assert (
job.updated_at > job.created_at
), f"updated at should not be the same as created at"
if loss >= prev_loss:
return
print(job)
assert job.active == True, f"job should be active, currently is {job.active}"
assert (
job.best_loss == loss
), f"best loss should be {loss}, currently is {job.best_loss}"
assert (
job.best_hotkey == hotkey
), f"best hotkey should be {hotkey}, currently is {job.best_hotkey}"
def test_init_store():
store = SQLiteJobStore(db_path=DB_PATH)
# Check if database is empty
with sqlite3.connect(store.db_file) as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT COUNT(*) FROM {store.table_name}")
count = cursor.fetchone()[0]
assert count == 0, "store should be empty on initialization"
@pytest.mark.parametrize("mock", [True, False])
def test_insert_single_job_into_store(mock):
store = SQLiteJobStore(db_path=DB_PATH)
info = {
"pdb": PDB,
"ff": FF,
"box": BOX,
"water": WATER,
"hotkeys": ["a", "b", "c", "d"],
"created_at": time.time(),
"updated_at": time.time(),
}
if mock:
job = MockJob()
else:
job = Job(**info)
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
# Check database is not empty
with sqlite3.connect(store.db_file) as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT COUNT(*) FROM {store.table_name}")
count = cursor.fetchone()[0]
assert count > 0, "store should not be empty after inserting a job"
assert (
store.get_queue(ready=False).qsize() == 1
), f"queue should have one job, currently has {store.get_queue(ready=False).qsize()}"
def test_repeat_insert_same_pdb_fails():
store = SQLiteJobStore(db_path=DB_PATH)
job = MockJob()
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
# This should fail because the pdb is already in the store
with pytest.raises(ValueError):
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
@pytest.mark.parametrize("n", [0, 1, 10, 100])
def test_save_then_load_store(n):
# Create first store and insert jobs
store1 = SQLiteJobStore(db_path=DB_PATH)
jobs = []
for i in range(n):
job = MockJob()
store1.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
jobs.append(job)
# Create second store pointing to same database
store2 = SQLiteJobStore(db_path=DB_PATH)
# Compare contents
with sqlite3.connect(store1.db_file) as conn:
df1 = pd.read_sql_query(f"SELECT * FROM {store1.table_name}", conn)
with sqlite3.connect(store2.db_file) as conn:
df2 = pd.read_sql_query(f"SELECT * FROM {store2.table_name}", conn)
pd.testing.assert_frame_equal(df1, df2)
assert len(df1) == n, f"store should have {n} jobs, has {len(df1)}"
@pytest.mark.parametrize("ready", [True, False])
@pytest.mark.parametrize("update_seconds", [0, 10])
def test_queue_contains_jobs(ready, update_seconds):
store = SQLiteJobStore(db_path=DB_PATH)
t0 = time.time()
for i in range(10):
job = MockJob(update_seconds=update_seconds)
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
elapsed = time.time() - t0
queue = store.get_queue(ready=ready)
if ready and elapsed < update_seconds:
assert (
queue.qsize() == 0
), f"queue should have 0 jobs, currently has {queue.qsize()}"
assert (
store.get_queue(ready=False).qsize() == 10
), f"queue should have 10 jobs, currently has {store.get_queue(ready=False).qsize()}"
def test_queue_is_empty_when_all_jobs_are_complete():
store = SQLiteJobStore(db_path=DB_PATH)
for i in range(10):
job = MockJob()
job.active = False
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
active=job.active,
)
assert (
store.get_queue(ready=False).qsize() == 0
), f"queue should be empty, currently has {store.get_queue(ready=False).qsize()}"
@pytest.mark.asyncio()
async def test_job_update_in_store():
store = SQLiteJobStore(db_path=DB_PATH)
job = MockJob()
# Insert initial job
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
# Update job
new_loss = -0.1
await job.update(loss=new_loss, hotkey=job.hotkeys[0])
store.update(job)
# Retrieve and check updated job
with sqlite3.connect(store.db_file) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute(
f"SELECT best_loss FROM {store.table_name} WHERE pdb = ?", (job.pdb,)
)
stored_loss = cursor.fetchone()["best_loss"]
assert (
stored_loss == new_loss
), f"stored loss should be {new_loss}, got {stored_loss}"
def test_store_handles_complex_data_types():
store = SQLiteJobStore(db_path=DB_PATH)
# Create job with complex data types
job = MockJob()
job.event = {"type": "test", "data": [1, 2, 3]}
job.system_kwargs = {"param1": "value1", "param2": [4, 5, 6]}
# Insert and retrieve
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
queue = store.get_queue(ready=False)
retrieved_job = queue.get()
assert retrieved_job.event == job.event, "Complex event data not preserved"
assert (
retrieved_job.system_kwargs == job.system_kwargs
), "Complex system_kwargs not preserved"
def test_get_all_pdbs():
store = SQLiteJobStore(db_path=DB_PATH)
# Insert some mock jobs
expected_pdbs = []
for _ in range(5):
job = MockJob()
expected_pdbs.append(job.pdb)
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
# Get all PDBs
pdbs = store.get_all_pdbs()
# Check results
assert len(pdbs) == 5, f"Expected 5 PDBs, got {len(pdbs)}"
assert set(pdbs) == set(expected_pdbs), "Retrieved PDBs don't match expected ones"
@pytest.mark.asyncio()
async def insert_jobs(store, jobs):
for job in jobs:
store.insert(
pdb=job.pdb,
ff=job.ff,
water=job.water,
box=job.box,
hotkeys=job.hotkeys,
epsilon=job.epsilon,
system_kwargs=job.system_kwargs,
event=job.event,
)
@pytest.mark.asyncio()
async def test_simul_write():
store = SQLiteJobStore(db_path=DB_PATH)
jobs = [MockJob() for _ in range(100)]
jobs2 = [MockJob() for _ in range(100)]
task1 = asyncio.create_task(insert_jobs(store, jobs))
task2 = asyncio.create_task(insert_jobs(store, jobs2))
await asyncio.gather(task1, task2)
assert (
store.get_queue(ready=False).qsize() == 200
), f"queue should have 200 jobs, currently has {store.get_queue(ready=False).qsize()}"
for job, job2 in zip(jobs, jobs2):
assert job.pdb in store.get_all_pdbs(), f"job {job.pdb} not found in store"
assert job2.pdb in store.get_all_pdbs(), f"job {job2.pdb} not found in store"