1+ # See ../README.md for instructions on running tests.
2+ import shutil
3+
4+ from hapiclient .hapi import hapi
5+
6+ from util import compare
7+
8+ from util .get_logger import get_logger
9+ logger = get_logger (__name__ )
10+
11+ kwargs = {
12+ 'cache' : False ,
13+ 'usecache' : False ,
14+ 'cachedir' : '/tmp/hapi-data' ,
15+ 'logging' : False
16+ }
17+
18+ server = 'http://hapi-server.org/servers/TestData2.0/hapi'
19+ dataset = 'dataset1'
20+ start = '1970-01-01'
21+ stop = '1970-01-01T00:00:03'
22+
23+ def test_cache_short ():
24+
25+ # Compare read with empty cache with read with hot cache and usecache=True
26+
27+ opts = {** kwargs , 'cache' : True }
28+
29+ opts ['usecache' ] = False
30+ shutil .rmtree (opts ['cachedir' ], ignore_errors = True )
31+ data , _ = hapi (server , dataset , 'scalarint,vectorint' , start , stop , ** opts )
32+
33+ opts ['usecache' ] = True
34+ data2 , _ = hapi (server , dataset , 'scalarint,vectorint' , start , stop , ** opts )
35+
36+ assert compare .equal (data , data2 )
37+
38+
39+ def test_cache_error ():
40+
41+ from unittest .mock import patch
42+
43+ import io
44+ import contextlib
45+ import pathlib
46+ import tempfile
47+ from hapiclient .util import write_atomic
48+
49+ def assert_warns (fn , expected ):
50+ buf = io .StringIO ()
51+ with contextlib .redirect_stderr (buf ):
52+ result = fn ()
53+ print (buf .getvalue ())
54+ msg = f"Expected '{ expected } ' in stderr: { buf .getvalue ()!r} "
55+ assert expected in buf .getvalue (), msg
56+ return result
57+
58+ # Direct calls to write_atomic()
59+ with tempfile .TemporaryDirectory () as tmpdir :
60+ path = pathlib .Path (tmpdir ) / 'test.json'
61+ data = {'key' : 'value' }
62+
63+ def call_write_atomic ():
64+ write_atomic (str (path ), data )
65+
66+ # Simulate write failure
67+ with patch ('json.dump' , side_effect = OSError ('No space left on device' )):
68+ assert_warns (call_write_atomic , 'Failed to write cache file' )
69+ assert not path .exists (), 'File should not exist after write failure'
70+
71+ # Simulate os.replace failure after successful write
72+ with patch ('os.replace' , side_effect = OSError ('Permission denied' )):
73+ assert_warns (call_write_atomic , 'Failed to rename cache file from' )
74+ assert not path .exists (), 'File should not exist after rename failure'
75+
76+ # Simulate write failure AND unlink failure
77+ patch1 = patch ('json.dump' , side_effect = OSError ('No space left on device' ))
78+ patch2 = patch ('pathlib.Path.unlink' , side_effect = OSError ('Permission denied' ))
79+ with patch1 , patch2 :
80+ assert_warns (call_write_atomic , 'Failed to remove temporary cache file' )
81+
82+ # Simulate successful write
83+ write_atomic (str (path ), data )
84+ assert path .exists (), 'File should exist after successful write'
85+
86+
87+ # Indirect calls to write_atomic()
88+ dataset = 'dataset1'
89+ start = '1970-01-01'
90+ stop = '1970-01-01T00:00:03'
91+
92+ opts = {** kwargs , 'cache' : True , 'usecache' : False }
93+
94+ def call_hapi ():
95+ data , meta = hapi (server , dataset , 'scalarint,vectorint' , start , stop , ** opts )
96+ return data , meta
97+
98+ def assert_data_valid (data ):
99+ msg = hapi (server , dataset , 'scalarint,vectorint' , start , stop , ** opts )
100+ assert data ['Time' ][0 ] == b'1970-01-01T00:00:00.000Z' , msg
101+
102+ with patch ('pickle.dump' , side_effect = OSError ('No space left on device' )):
103+ data , _ = assert_warns (call_hapi , 'Failed to write cache file' )
104+ assert_data_valid (data )
105+
106+ with patch ('os.replace' , side_effect = OSError ('Permission denied' )):
107+ data , _ = assert_warns (call_hapi , 'Failed to rename cache file from' )
108+ assert_data_valid (data )
109+
110+ p1 = patch ('pickle.dump' , side_effect = OSError ('No space left on device' ))
111+ p2 = patch ('pathlib.Path.unlink' , side_effect = OSError ('Permission denied' ))
112+ with p1 , p2 :
113+ data , _ = assert_warns (call_hapi , 'Failed to remove temporary cache file' )
114+ assert_data_valid (data )
115+
116+
117+ if __name__ == '__main__' :
118+ test_cache_short ()
119+ test_cache_error ()
0 commit comments