Skip to content

Commit 7c495b6

Browse files
author
WGussev
committed
add unit testing
1 parent 0a33c82 commit 7c495b6

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

Diff for: power_watcher/power_watcher.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def update(self, value):
5050
self.value = value
5151

5252

53-
def _watch_power(logfile: Path = None, sender: Connection = None, display: bool = True):
53+
def _watch_power(logfile: Path = None, sender: Connection = None, display: bool = False):
5454
"""
5555
Poll GPU and log/display current power consumption.
5656
Update frequency: every 1 second.
@@ -60,8 +60,6 @@ def _watch_power(logfile: Path = None, sender: Connection = None, display: bool
6060
:param display: display consumption in terminal.
6161
:return: None
6262
"""
63-
if (logfile is None) and (display is False):
64-
raise ValueError('You should log and/or display consumption')
6563

6664
total = 0
6765
killer = _GracefulKiller()
@@ -79,6 +77,7 @@ def _watch_power(logfile: Path = None, sender: Connection = None, display: bool
7977
if logfile is not None:
8078
f.write(f'{datetime.now()} {power}\n')
8179
time.sleep(1)
80+
print(total)
8281
if display:
8382
print('', end='\n')
8483
if sender is not None:
@@ -97,10 +96,10 @@ class PowerWatcher:
9796
pw.total # get results
9897
"""
9998

100-
def __init__(self, logfile: Path = None, display: bool = True):
99+
def __init__(self, logfile: Path = None, display: bool = False):
101100
"""
102101
:param logfile: logfile path.
103-
:param display: consumption display toggle.
102+
:param display: display consumption in the terminal.
104103
"""
105104
self.logfile = logfile
106105
self.display = display
@@ -130,4 +129,4 @@ def start(self):
130129
def stop(self):
131130
"""Stop manually."""
132131
self.watcher.terminate()
133-
self.total.update(self.recv_end.recv())
132+
self.total.update(self.recv_end.recv())

Diff for: test.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import unittest
2+
from importlib import import_module
3+
from multiprocessing import Process, Pipe
4+
from time import sleep
5+
from unittest.mock import patch
6+
7+
8+
class FirstTest(unittest.TestCase):
9+
pw = import_module('power_watcher')
10+
pw = pw.power_watcher
11+
12+
def test_value_container(self):
13+
container = self.pw._ValueContainer()
14+
self.assertEqual(container.value, 0)
15+
container.update(7)
16+
self.assertEqual(container.value, 7)
17+
container.update(0.45)
18+
self.assertEqual(container.value, 0.45)
19+
20+
def test_process_killer(self):
21+
def f(sender):
22+
killer = self.pw._GracefulKiller()
23+
while killer.kill_now is False:
24+
pass
25+
sender.send(killer)
26+
27+
recv_end, send_end = Pipe(False)
28+
watcher = Process(target=f, args=(send_end,))
29+
watcher.start()
30+
sleep(1)
31+
watcher.terminate()
32+
kn = recv_end.recv()
33+
self.assertEqual(kn.kill_now, True)
34+
35+
@patch('power_watcher.power_watcher.nvmlInit')
36+
@patch('power_watcher.power_watcher.nvmlDeviceGetHandleByIndex')
37+
@patch('power_watcher.power_watcher.nvmlDeviceGetPowerUsage', return_value=3600 * 1000 * 1000)
38+
def test_context_manager(self, mock_init, mock_handle, mock_power):
39+
recv_end, send_end = Pipe(False)
40+
watcher = Process(target=self.pw._watch_power, args=(None, send_end, False))
41+
watcher.start()
42+
sleep(1)
43+
watcher.terminate()
44+
self.assertEqual(recv_end.recv(), 1.0)

0 commit comments

Comments
 (0)