-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_weblablib.py
1522 lines (1199 loc) · 52.3 KB
/
test_weblablib.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import json
import time
import base64
import datetime
import requests
import threading
import six
if six.PY2:
from StringIO import StringIO
else:
from io import StringIO
from flask import Flask, url_for, render_template_string, g, session
import flask.cli as flask_cli
import weblablib
from weblablib.tasks import _TaskRunner
import unittest
from click.testing import CliRunner
try:
from flask_socketio import SocketIO, SocketIOTestClient
except ImportError:
print()
print("Important: if you do not have SocketIO you can't run these tests")
print()
raise
os.environ['FLASK_APP'] = 'fake.py' # Overrided later
cmp = lambda a, b: a.__cmp__(b)
class StdWrap(object):
def __enter__(self):
self.sysout = sys.stdout
self.syserr = sys.stderr
self.fake_stdout = sys.stdout = StringIO()
self.fake_stderr = sys.stderr = StringIO()
def __exit__(self, *args, **kwargs):
sys.stdout = self.sysout
sys.stderr = self.syserr
class WithoutWebLabTest(unittest.TestCase):
def test_extension(self):
app = Flask(__name__)
with self.assertRaises(weblablib.WebLabNotInitializedError):
with app.app_context():
weblablib._current_weblab()
class BaseWebLabTest(unittest.TestCase):
def get_config(self):
return {
'SECRET_KEY': 'super-secret',
'WEBLAB_USERNAME': 'weblabdeusto',
'WEBLAB_PASSWORD': 'password',
'SERVER_NAME': self.server_name,
'WEBLAB_SCHEME': 'https',
'WEBLAB_AUTOCLEAN_THREAD': False, # No thread
'WEBLAB_TASK_THREADS_PROCESS': 0, # No thread
}
def create_weblab(self):
weblablib._CleanerThread.created = False
self.weblab = weblablib.WebLab()
self.app = Flask(__name__)
flask_cli.locate_app = lambda *args: self.app
self.server_name = 'localhost:5000'
self.app.config.update(self.get_config())
self.auth_headers = {
'Authorization': 'Basic ' + base64.encodestring(b'weblabdeusto:password').decode('utf8').strip(),
}
self.wrong_auth_headers = {
'Authorization': 'Basic ' + base64.encodestring(b'wrong_weblabdeusto:wrong_password').decode('utf8').strip(),
}
@self.weblab.task(unique='global')
def task_before_init():
return True
self.weblab.init_app(self.app)
self.weblab._backend.client.flushall()
@self.weblab.on_start
def on_start(client_data, server_data):
self.on_start(client_data, server_data)
@self.weblab.on_dispose
def on_dispose():
self.on_dispose()
@self.weblab.initial_url
def initial_url():
return url_for('lab')
@self.app.route('/lab/')
@weblablib.requires_login
def lab():
return self.lab()
@self.app.route('/lab/active')
@weblablib.requires_active
def lab_active():
return self.lab()
@self.app.route('/logout')
@weblablib.requires_active
def logout():
weblablib.logout()
return "logout"
@self.app.route('/poll')
@weblablib.requires_active
def poll():
weblablib.poll()
weblablib.poll() # Twice so as to test g.poll_requested
return "poll"
@self.weblab.task()
def task():
return self.task()
with self.assertRaises(ValueError) as cm:
@self.weblab.task()
def task():
self.task()
self.assertIn("same name", str(cm.exception))
with self.assertRaises(ValueError) as cm:
# 43 characters is the length of create_token()
self.assertEquals(43, len(weblablib.create_token()))
# Therefore, having a task with that name length is forbidden
# to avoid any potential issue in get_task
@self.weblab.task()
def abc0123456789012345678901234567890123456789():
pass
self.assertIn("number of characters", str(cm.exception))
self.current_task = task
def get_json(self, rv):
return json.loads(rv.get_data(as_text=True))
def get_text(self, rv):
return rv.get_data(as_text=True)
def on_start(self, client_data, server_data):
pass
def on_dispose(self):
pass
def lab(self):
return ":-)"
def task(self):
pass
def setUp(self):
self.create_weblab()
def tearDown(self):
self.weblab._cleanup()
class WebLabApiTest(BaseWebLabTest):
def test_api(self):
with self.app.test_client() as client:
result = self.get_json(client.get('/weblab/sessions/api'))
self.assertEquals(result['api_version'], '1')
def test_weblab_test_without_auth(self):
with self.app.test_client() as client:
result = self.get_json(client.get('/weblab/sessions/test'))
self.assertEquals(result['valid'], False)
self.assertIn("no username", result['error_messages'][0])
def test_weblab_test_with_wrong_auth(self):
with StdWrap():
with self.app.test_client() as client:
result = self.get_json(client.get('/weblab/sessions/test', headers=self.wrong_auth_headers))
self.assertEquals(result['valid'], False)
self.assertIn("wrong username", result['error_messages'][0])
def test_weblab_test_with_right_auth(self):
with self.app.test_client() as client:
result = self.get_json(client.get('/weblab/sessions/test', headers=self.auth_headers))
self.assertEquals(result['valid'], True)
def test_weblab_status_with_wrong_auth(self):
with StdWrap():
with self.app.test_client() as client:
result = self.get_text(client.get('/weblab/sessions/<invalid>/status', headers=self.wrong_auth_headers))
self.assertIn("seem to be", result)
class SimpleUnauthenticatedTest(BaseWebLabTest):
def test_token(self):
token1 = self.weblab.create_token()
token2 = self.weblab.create_token()
self.assertNotEquals(token1, token2)
def test_task_not_found(self):
task = self.weblab.get_task("does.not.exist")
self.assertIsNone(task)
def test_callback_initial_url(self):
self.weblab._initial_url = None
with StdWrap():
with self.app.test_client() as client:
result = self.get_text(client.get('/callback/session.not.found'))
self.assertIn('ERROR', result)
self.assertIn('weblab.initial_url', result)
def test_callback(self):
with self.app.test_client() as client:
result = self.get_text(client.get('/callback/session.not.found'))
self.assertIn('forbidden', result)
def test_anonymous(self):
with self.app.test_client() as client:
client.get('/lab/')
self.assertTrue(weblablib.weblab_user.is_anonymous)
self.assertFalse(weblablib.weblab_user.active)
self.assertIsNone(weblablib.weblab_user.locale)
self.assertIsNotNone(weblablib.weblab_user.data)
with self.assertRaises(TypeError):
self.assertEquals(0, len(weblablib.weblab_user.data))
def test_anonymous_on_active(self):
with self.app.test_client() as client:
rv = client.get('/lab/active')
self.assertIn("forbidden", self.get_text(rv))
def test_poll_url(self):
with self.app.test_client() as client:
client.get('/lab/')
url = url_for('weblab_poll_url', session_id='does.not.exist')
result = self.get_json(client.get(url))
self.assertIn("Different session", result['reason'])
with client.session_transaction() as sess:
sess[self.weblab._session_id_name] = 'does.not.exist'
result = self.get_json(client.get(url))
self.assertIn("Not found", result['reason'])
def test_logout_url(self):
with self.app.test_client() as client:
client.get('/lab/')
url = url_for('weblab_logout_url', session_id='does.not.exist')
result = self.get_json(client.get(url))
self.assertIn("Different session", result['reason'])
def test_poll_script(self):
with self.app.test_client() as client:
client.get('/lab/')
result = render_template_string("{{ weblab_poll_script() }}")
self.assertIn('session_id not found', result)
def test_unauthorized(self):
with self.app.test_client() as client:
result = self.get_text(client.get('/lab/'))
self.assertIn("Access forbidden", result)
def test_dispose_wrong_requests(self):
with self.app.test_client() as client:
request_data = {
}
rv = client.post('/weblab/sessions/{}'.format('foo'), data=json.dumps(request_data), headers=self.auth_headers)
self.assertIn("Unknown", self.get_json(rv)['message'])
request_data = {
'action': 'look at the mountains'
}
rv = client.post('/weblab/sessions/{}'.format('foo'), data=json.dumps(request_data), headers=self.auth_headers)
self.assertIn("Unknown", self.get_json(rv)['message'])
request_data = {
'action': 'delete'
}
rv = client.post('/weblab/sessions/{}'.format('does.not.exist'), data=json.dumps(request_data), headers=self.auth_headers)
self.assertIn("Not found", self.get_json(rv)['message'])
class SimpleNoTimeoutUnauthenticatedTest(BaseWebLabTest):
def get_config(self):
config = super(SimpleNoTimeoutUnauthenticatedTest, self).get_config()
config['WEBLAB_TIMEOUT'] = 0
return config
def test_poll_script_timeout(self):
with self.app.test_client() as client:
client.get('/lab/')
result = render_template_string("{{ weblab_poll_script() }}")
self.assertIn('timeout is 0', result)
class UnauthorizedLinkSimpleTest(BaseWebLabTest):
def get_config(self):
config = super(UnauthorizedLinkSimpleTest, self).get_config()
config['WEBLAB_UNAUTHORIZED_LINK'] = 'http://mylink'
return config
def test_unauthorized_link(self):
with self.app.test_client() as client:
rv = client.get('/lab/', follow_redirects=False)
self.assertEquals(rv.location, 'http://mylink')
class UnauthorizedTemplateSimpleTest(BaseWebLabTest):
def get_config(self):
config = super(UnauthorizedTemplateSimpleTest, self).get_config()
config['WEBLAB_UNAUTHORIZED_TEMPLATE'] = 'mytemplate.html'
return config
def test_unauthorized_template(self):
def new_render_template(location):
return location
old_render_template = weblablib.render_template
with self.app.test_client() as client:
weblablib.render_template = new_render_template
try:
lab_result = self.get_text(client.get('/lab/'))
finally:
weblablib.render_template = old_render_template
self.assertEquals(lab_result, 'mytemplate.html')
class TestNewUserError(Exception):
pass
class BaseSessionWebLabTest(BaseWebLabTest):
def setUp(self):
super(BaseSessionWebLabTest, self).setUp()
# self.weblab_client is stateless, sessionless
self.weblab_client = self.app.test_client()
# while self.client represents the user web browser
self.client = self.app.test_client(use_cookies=True)
self.client.__enter__()
def tearDown(self):
self.client.__exit__(None, None, None)
super(BaseSessionWebLabTest, self).tearDown()
def new_user(self, name='Jim Smith', username='jim.smith', username_unique='jim.smith@labsland',
assigned_time=300, back='http://weblab.deusto.es', language='en',
experiment_name='mylab', category_name='Lab experiments', use_timestamp=True):
assigned_time = float(assigned_time)
now = datetime.datetime.now().replace(microsecond=0)
start_time = now.strftime("%Y-%m-%d %H:%M:%S") + '.0'
self._start_time_float = _to_timestamp(now)
request_data = {
'client_initial_data': {
'in_test': True,
},
'server_initial_data': {
'priority.queue.slot.start': start_time,
'priority.queue.slot.length': assigned_time,
'request.username': username,
'request.full_name': name,
'request.username.unique': username_unique,
'request.locale': language,
'request.experiment_id.experiment_name': experiment_name,
'request.experiment_id.category_name': category_name,
},
'back': back,
}
if use_timestamp:
request_data['server_initial_data']['priority.queue.slot.start.timestamp'] = '%s' % self._start_time_float
rv = self.weblab_client.post('/weblab/sessions/', data=json.dumps(request_data), headers=self.auth_headers)
response = self.get_json(rv)
if 'session_id' in response:
self.session_id = response['session_id']
launch_url = response['url']
relative_launch_url = launch_url.split(self.server_name, 1)[1]
return launch_url, self.session_id
raise TestNewUserError(response['message'])
def status(self, session_id = None):
if session_id is None:
session_id = self.session_id
rv = self.weblab_client.get('/weblab/sessions/{}/status'.format(session_id), headers=self.auth_headers)
return self.get_json(rv)
def dispose(self, session_id = None):
if session_id is None:
session_id = self.session_id
request_data = {
'action': 'delete',
}
rv = self.weblab_client.post('/weblab/sessions/{}'.format(session_id), data=json.dumps(request_data), headers=self.auth_headers)
return self.get_json(rv)
class UserTest(BaseSessionWebLabTest):
def lab(self):
task = self.current_task.delay()
# check that it's a dictionary
if weblablib.weblab_user.active:
weblablib.weblab_user.data['foo'] = 'bar'
# And in any case build another
if weblablib.weblab_user.active:
# Optional, forces an immediate synchronization
weblablib.weblab_user.data = {'foo': 'bar'}
return render_template_string("""@@task@@%s@@task@@{{ weblab_poll_script() }}
{{ weblab_poll_script(logout_on_close=True, callback='myfunc') }}""" % task.task_id)
def task(self):
self.counter += 1
if not weblablib.current_task_stopping:
return "ERROR"
weblablib.current_task.data = {'inside': 'previous'}
weblablib.current_task.update_data({'inside': 'yes'})
weblablib.current_task.store()
weblablib.weblab_user.data.retrieve()
return [ self.counter, weblablib.weblab_user.data['foo'] ]
def test_simple(self):
# New user
launch_url1, session_id1 = self.new_user(language='es_ES')
# counter is zero
self.counter = 0
# We call the relative_launch_url. It is redirected to the lab, which
# starts a new task, which establishes that counter is zero
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
self.assertEquals(weblablib.weblab_user.session_id, session_id1)
self.assertEquals(weblablib.weblab_user.locale, 'es_ES')
self.assertEquals(weblablib.weblab_user.full_name, 'Jim Smith')
self.assertEquals(weblablib.weblab_user.experiment_name, 'mylab')
self.assertEquals(weblablib.weblab_user.category_name, 'Lab experiments')
self.assertEquals(weblablib.weblab_user.experiment_id, 'mylab@Lab experiments')
self.assertEquals(weblablib.weblab_user.request_client_data['in_test'], True)
self.assertEquals(weblablib.weblab_user.request_server_data['request.username'], 'jim.smith')
self.assertEquals(weblablib.weblab_user.start_date, float(self._start_time_float))
task_id = response.split('@@task@@')[1]
# There is one task, which is running
self.assertEquals(len(self.weblab.tasks), 1)
self.assertEquals(len(self.weblab.running_tasks), 1)
task1 = self.weblab.get_task(task_id)
self.assertIsNotNone(task1)
self.assertEquals(task1.name, 'task')
self.assertEquals(task1.status, 'submitted')
self.assertTrue(task1.submitted)
self.assertFalse(task1.finished)
self.assertFalse(task1.stopping)
self.assertFalse(task1.done)
self.assertFalse(task1.failed)
self.assertFalse(task1.running)
self.assertEquals(task1.session_id, session_id1)
self.assertIsNone(task1.result)
self.assertIsNone(task1.error)
task1.stop()
task1.retrieve()
self.assertTrue(task1.stopping)
task1b = self.weblab.get_task(self.current_task)
task1c = self.weblab.get_task('task')
task1d = self.weblab.get_running_task(self.current_task)
task1e = self.weblab.get_running_task('task')
self.assertEquals(task1b, task1)
self.assertEquals(task1c, task1)
self.assertEquals(task1d, task1)
self.assertEquals(task1e, task1)
tasks_b = self.weblab.get_tasks(self.current_task)
tasks_c = self.weblab.get_tasks('task')
tasks_d = self.weblab.get_running_tasks(self.current_task)
tasks_e = self.weblab.get_running_tasks('task')
self.assertEquals(1, len(tasks_b))
self.assertEquals(1, len(tasks_c))
self.assertEquals(1, len(tasks_d))
self.assertEquals(1, len(tasks_e))
self.assertEquals(0, len(self.weblab.get_tasks('foo')))
self.assertEquals(0, len(self.weblab.get_running_tasks('foo')))
self.assertEquals(tasks_b[0], task1)
self.assertEquals(tasks_c[0], task1)
self.assertEquals(tasks_d[0], task1)
self.assertEquals(tasks_e[0], task1)
# We're outside a task
self.assertFalse(weblablib.current_task_stopping)
self.weblab.join_tasks(self.current_task, timeout=0.01, stop=True)
# But the counter is still zero
self.assertEquals(self.counter, 0)
global started
started = False
def wait_for_thread():
global started
started = True
task1.join(timeout=3)
background_thread = threading.Thread(target=wait_for_thread)
background_thread.daemon = True
background_thread.start()
initial = time.time()
while not started:
time.sleep(0.05)
if time.time() - initial > 2:
self.fail("Error waiting for thread to start")
# Run the tasks
self.weblab.run_tasks()
background_thread.join(timeout=5)
self.assertFalse(background_thread.isAlive())
# The task has been run
self.assertEquals(self.counter, 1)
# There is still 1 task in this session, but no running task
self.assertEquals(len(self.weblab.tasks), 1)
self.assertEquals(len(self.weblab.running_tasks), 0)
# Let's retrieve the task again
task2 = self.weblab.get_task(task_id)
self.assertEquals(task2.status, 'done')
self.assertTrue(task2.done)
self.assertTrue(task2.finished)
self.assertFalse(task2.failed)
self.assertFalse(task2.submitted)
self.assertFalse(task2.running)
self.assertIsNone(task2.error)
self.assertEquals(task2.result, [1, 'bar'])
self.assertIn('inside', task2.data)
self.assertEquals(task2.data['inside'], 'yes')
self.assertFalse(weblablib.current_task)
# And let's see how it's the same task as before
self.assertEquals(task1, task2)
self.assertEquals(hash(task1), hash(task2))
self.assertEquals(cmp(task1, task2), 0)
self.assertFalse(task1 < task2)
self.assertFalse(task2 < task1)
task2b = self.weblab.get_task(self.current_task)
task2c = self.weblab.get_task('task')
task2d = self.weblab.get_running_task(self.current_task)
task2e = self.weblab.get_running_task('task')
self.assertEquals(task2b, task2)
self.assertEquals(task2c, task2)
self.assertIsNone(task2d)
self.assertIsNone(task2e)
# sys.maxint/maxsize is the maximum integer. Any hash will be lower than that
# (except for if suddenly the random string is exactly maxint...)
if six.PY2:
maxvalue = sys.maxint
self.assertTrue(task1 < maxvalue)
# In python 3 it's quite difficult to find the largest hashable value
# ( sys.hash_info.modulus - 1 is the largest number where hash(n) == n, but
# for many other hash(x) > hash(2 ^ 61 - 1))
self.assertIn(task1.task_id, repr(task1))
self.assertNotEquals(task1, task1.task_id)
self.assertNotEquals(cmp(task1, task1.task_id), 0)
# Cool!
self.client.get(url_for('weblab_poll_url', session_id=session_id1))
self.client.get('/poll')
self.client.get('/logout')
# Even before cleaning expired users
rv = self.client.get('/lab/active')
self.assertEquals(rv.location, 'http://weblab.deusto.es')
self.weblab.clean_expired_users()
self.status(session_id1)
self.dispose(session_id1)
rv = self.client.get('/lab/active')
self.assertEquals(rv.location, 'http://weblab.deusto.es')
self.client.get('/lab/')
self.assertFalse(weblablib.weblab_user.active)
self.assertFalse(weblablib.weblab_user.is_anonymous)
self.assertEquals(weblablib.weblab_user.time_left, 0)
self.assertEquals(weblablib.weblab_user.session_id, session_id1)
self.assertIn(session_id1, str(weblablib.weblab_user))
with self.assertRaises(NotImplementedError):
weblablib.weblab_user.data = {}
with self.assertRaises(NotImplementedError):
weblablib.weblab_user.update_data()
self.assertEquals(weblablib.weblab_user.locale, 'es_ES')
self.assertEquals(weblablib.weblab_user.full_name, 'Jim Smith')
self.assertEquals(weblablib.weblab_user.experiment_name, 'mylab')
self.assertEquals(weblablib.weblab_user.category_name, 'Lab experiments')
self.assertEquals(weblablib.weblab_user.experiment_id, 'mylab@Lab experiments')
def test_status_concrete_time_left_without_timestamp(self):
# New user, with 3 seconds
launch_url1, session_id1 = self.new_user(assigned_time=3, use_timestamp=False)
should_finish = self.status()['should_finish']
# Ask in 2..3 seconds (not 5)
self.assertGreaterEqual(should_finish, 1)
self.assertLessEqual(should_finish, 3)
def test_status_concrete_time_left(self):
# New user, with 3 seconds
launch_url1, session_id1 = self.new_user(assigned_time=3)
should_finish = self.status()['should_finish']
# Ask in 2..3 seconds (not 5)
self.assertGreaterEqual(should_finish, 1)
self.assertLessEqual(should_finish, 3)
def test_status_exited(self):
# New user, with 3 seconds
launch_url1, session_id1 = self.new_user(assigned_time=3)
self.client.get(launch_url1, follow_redirects=True)
self.client.get(url_for('weblab_logout_url', session_id=session_id1))
should_finish = self.status()['should_finish']
# Logged out
self.assertEquals(should_finish, -1)
def test_status_time_left_passed(self):
# New user, with 3 seconds
launch_url1, session_id1 = self.new_user(assigned_time=0.1)
self.client.get(launch_url1, follow_redirects=True)
time.sleep(0.2)
should_finish = self.status()['should_finish']
# time passed
self.assertEquals(should_finish, -1)
def test_status_timeout(self):
# New user, with 3 seconds
self.weblab.timeout = 0.1
launch_url1, session_id1 = self.new_user()
self.client.get(launch_url1, follow_redirects=True)
time.sleep(0.2)
should_finish = self.status()['should_finish']
# time passed
self.assertEquals(should_finish, -1)
class TaskFailTest(BaseSessionWebLabTest):
def lab(self):
task = self.current_task.delay()
return str(task.task_id)
def task(self):
10 / 0
return -1
def test_task_fail(self):
# New user
launch_url1, session_id1 = self.new_user()
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
task_id = response
task = self.weblab.get_task(response)
self.assertEquals(task.status, 'submitted')
self.assertTrue(task.submitted)
self.assertFalse(task.failed)
self.assertFalse(task.finished)
self.assertFalse(task.stopping)
self.assertFalse(task.running)
self.assertFalse(task.done)
with StdWrap():
self.weblab.run_tasks()
task.retrieve()
self.assertEquals(task.status, 'failed')
self.assertTrue(task.failed)
self.assertTrue(task.finished)
self.assertFalse(task.done)
self.assertFalse(task.running)
self.assertFalse(task.submitted)
self.assertIsNone(task.result)
self.assertIsNotNone(task.error)
self.assertEqual(task.error['code'], 'exception')
self.assertIn('zero', task.error['message'])
self.assertEqual(task.error['class'], 'ZeroDivisionError')
class TaskJoinTimeoutTest(BaseSessionWebLabTest):
def lab(self):
task = self.current_task.delay()
return str(task.task_id)
def task(self):
return -1
def test_task_join_timeout(self):
# New user
launch_url1, session_id1 = self.new_user()
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
task_id = response
task = self.weblab.get_task(response)
# Task has not started. Perfect for running joins with timeout:
with self.assertRaises(weblablib.TimeoutError):
task.join(timeout=0.1)
self.assertFalse(task.finished)
task.join(timeout=0.1, error_on_timeout=False)
self.assertFalse(task.finished)
class TaskJoinSameThreadTest(BaseSessionWebLabTest):
def lab(self):
task = self.current_task.delay()
return str(task.task_id)
def task(self):
weblablib.current_task.join(timeout=0.5)
def test_task_join_timeout(self):
# New user
launch_url1, session_id1 = self.new_user()
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
task_id = response
with StdWrap():
self.weblab.run_tasks()
task = self.weblab.get_task(response)
self.assertTrue(task.failed)
self.assertEquals('exception', task.error['code'])
self.assertEquals('RuntimeError', task.error['class'])
self.assertIn('Deadlock', task.error['message'])
class MyLabUser(object):
def __init__(self, username_unique, username):
self.username_unique = username_unique
self.username = username
class LoadUserTest(BaseSessionWebLabTest):
def test_load_no_loader(self):
launch_url1, session_id1 = self.new_user()
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
user = weblablib.weblab_user.user
self.assertIsNone(user)
def test_load_simple(self):
@self.weblab.user_loader
def user_loader(username_unique):
return MyLabUser(username_unique, weblablib.weblab_user.username)
launch_url1, session_id1 = self.new_user(username='user1', username_unique='unique1')
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
user = weblablib.weblab_user.user
self.assertIsInstance(user, MyLabUser)
self.assertEquals(user.username, 'user1')
self.assertEquals(user.username_unique, 'unique1')
user2 = weblablib.weblab_user.user
self.assertIsInstance(user2, MyLabUser)
self.assertEquals(user2.username, 'user1')
self.assertEquals(user2.username_unique, 'unique1')
launch_url2, session_id2 = self.new_user(username='user2', username_unique='unique2')
response = self.get_text(self.client.get(launch_url2, follow_redirects=True))
user3 = weblablib.weblab_user.user
self.assertIsInstance(user3, MyLabUser)
self.assertEquals(user3.username, 'user2')
self.assertEquals(user3.username_unique, 'unique2')
def test_user_loader_fail(self):
@self.weblab.user_loader
def user_loader(username_unique):
raise Exception("Random error")
launch_url1, session_id1 = self.new_user(username='user1', username_unique='unique1')
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
with self.assertRaises(Exception) as cm:
weblablib.weblab_user.user
self.assertIn("Random error", str(cm.exception))
class LongTaskTest(BaseSessionWebLabTest):
def get_config(self):
config = super(LongTaskTest, self).get_config()
config['WEBLAB_AUTOCLEAN_THREAD'] = True
config['WEBLAB_TASK_THREADS_PROCESS'] = 3
return config
def lab(self):
if self.run_delayed:
task_object = self.current_task.delay()
else:
task_object = self.current_task.run_sync(timeout=self.sync_timeout)
return str(task_object.task_id)
def task(self):
time.sleep(self.sleep_time)
return 0
def test_long_task(self):
self.sleep_time = 0.6
self.weblab.cleaner_thread_interval = 0.1
self.run_delayed = True
launch_url1, session_id1 = self.new_user()
response = self.get_text(self.client.get(launch_url1, follow_redirects=True))
task_id = response
max_time = 3
t0 = time.time()
while True:
task = self.weblab.get_task(task_id)
# Temporal variable since sometimes the change happens
# between task.status and the next task.status
task_status = task.status
if task_status == 'running':
self.assertTrue(task.running)
self.assertFalse(task.finished)
self.assertFalse(task.done)
self.assertFalse(task.failed)
self.assertFalse(task.submitted)
break
self.assertEquals(task_status, 'submitted')
time.sleep(0.03)
if time.time() - t0 > max_time:
self.fail("Too long checking for a submitted thread")
self.client.get('/logout')
time.sleep(0.2) # So other thread calls clean
self.dispose()
weblablib._on_exit()
def test_run_sync_short(self):
self.sleep_time = 0.1
self.weblab.cleaner_thread_interval = 0.1
self.run_delayed = False
self.sync_timeout = None # Forever
launch_url1, session_id1 = self.new_user()
task_id = self.get_text(self.client.get(launch_url1, follow_redirects=True))
task = self.weblab.get_task(task_id)
self.assertTrue(task.done)
self.weblab._cleanup()
def test_run_sync_long(self):
self.sleep_time = 0.4
self.weblab.cleaner_thread_interval = 0.1
self.run_delayed = False
self.sync_timeout = 0.1 # Forever
launch_url1, session_id1 = self.new_user()
task_id = self.get_text(self.client.get(launch_url1, follow_redirects=True))
task = self.weblab.get_task(task_id)
self.assertFalse(task.done)
task.join()
self.assertTrue(task.done)
self.weblab._cleanup()
class GlobalUniqueTaskTest(BaseSessionWebLabTest):
def get_config(self):
config = super(GlobalUniqueTaskTest, self).get_config()
config['WEBLAB_AUTOCLEAN_THREAD'] = True
config['WEBLAB_TASK_THREADS_PROCESS'] = 3
return config
def lab(self):
# We can't know which one will be running first, so we force one to be running first
task_object = self.unique_task.delay()
while not self.task_is_running:
time.sleep(0.05)
# Now we now that the task is running. Let's run it again
task2 = self.unique_task.delay()
task2.join() # Should be fast
if not task2.failed:
raise Exception("unique=global should have avoided the second task")
if task2.error['class'] != 'AlreadyRunningError':
raise Exception("unique=global expected an AlreadyRunningError; got {}".format(task2.error))
return str(task_object.task_id)
def task(self):
initial = time.time()
self.task_is_running = True
while not weblablib.current_task_stopping:
time.sleep(0.05)
elapsed = time.time() - initial
if elapsed > 6:
raise Exception("Error, nobody calling stop")
def test_global_unique(self):
original_steps_waiting = _TaskRunner._STEPS_WAITING
_TaskRunner._STEPS_WAITING = 1
try:
self.weblab.cleaner_thread_interval = 0.1
self.task_is_running = False
@self.weblab.task(unique='global')
def task_global_unique():
return self.task()
self.unique_task = task_global_unique
launch_url1, session_id1 = self.new_user()
with StdWrap():
task_id = self.get_text(self.client.get(launch_url1, follow_redirects=True))
task = self.weblab.get_task(task_id)
task.stop()
task.join()
self.assertTrue(task.done)
finally:
_TaskRunner._STEPS_WAITING = original_steps_waiting
class UserUniqueTaskTest(BaseSessionWebLabTest):
def get_config(self):
config = super(UserUniqueTaskTest, self).get_config()
config['WEBLAB_AUTOCLEAN_THREAD'] = True
config['WEBLAB_TASK_THREADS_PROCESS'] = 3
return config
def lab(self):
# We can't know which one will be running first, so we force one to be running first
task_object = self.unique_task.delay()
while not self.task_is_running:
time.sleep(0.05)
task2 = self.unique_task.delay()
task2.join() # Should be fast
if not task2.failed:
raise Exception("unique=user should have avoided the second task")
if task2.error['class'] != 'AlreadyRunningError':
raise Exception("unique=user expected an AlreadyRunningError; got {}".format(task2.error))
return str(task_object.task_id)
def task(self):
initial = time.time()
self.task_is_running = True
while not weblablib.current_task_stopping:
time.sleep(0.05)
elapsed = time.time() - initial
if elapsed > 6:
raise Exception("Error, nobody calling stop")
def test_user_unique(self):
self.weblab.cleaner_thread_interval = 0.1
self.task_is_running = False
@self.weblab.task(unique='user')