-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmux_test.py
201 lines (163 loc) · 8.24 KB
/
tmux_test.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
import datetime
import unittest
from unittest import mock
import trackd
from trackd import SpanTracker
from tmux import TmuxClient, TmuxClientSessionMap, TmuxSession, TmuxAdapter
class TmuxClientSessionMapTest(unittest.TestCase):
def test_session_renamed(self):
session_map = TmuxClientSessionMap()
client_foo = TmuxClient(client_name='foo', hostname='host')
client_bar = TmuxClient(client_name='bar', hostname='host')
old_session = TmuxSession(session_name='old', hostname='host', server_pid=42)
new_session = TmuxSession(session_name='new', hostname='host', server_pid=42)
# Set both clients to the same session.
session_map[client_foo] = old_session
session_map[client_bar] = old_session
# Trigger session rename for one client.
session_map.session_renamed(client=client_foo, new_session=new_session)
# Check both clients now point to the new session.
self.assertEqual(session_map[client_foo], new_session)
self.assertEqual(session_map[client_bar], new_session)
def test_session_closed(self):
session_map = TmuxClientSessionMap()
client_foo = TmuxClient(client_name='foo', hostname='host')
client_bar = TmuxClient(client_name='bar', hostname='host')
session = TmuxSession(session_name='session', hostname='host', server_pid=42)
# Set both clients to the same session.
session_map[client_foo] = session
session_map[client_bar] = session
# Signal session closure.
session_map.session_closed(session)
# Both clients shouldn't have an assigned session anymore.
self.assertNotIn(client_foo, session_map)
self.assertNotIn(client_bar, session_map)
class FakeSpanStorage:
def __init__(self):
self.spans = []
def add(self, span):
self.spans.append(span)
class TmuxAdapterTest(unittest.TestCase):
def setUp(self):
self.span_storage = FakeSpanStorage()
self.span_tracker = SpanTracker(self.span_storage)
self.adapter = TmuxAdapter(self.span_tracker)
self.now_patcher = mock.patch.object(trackd, 'now')
self.now = self.now_patcher.start()
self.set_now(0)
def tearDown(self):
self.now_patcher.stop()
def set_now(self, timestamp: int):
self.now.return_value = datetime.datetime.fromtimestamp(timestamp)
def test_session_closed(self):
x_window_id = 42
client = TmuxClient(client_name='client', hostname='host')
session = TmuxSession(session_name='session', hostname='host', server_pid=42)
# Activate a session in the adapter.
self.set_now(0)
self.adapter.set_focused_x_window_id(x_window_id, 'Terminal')
self.adapter.set_client_for_x_window_id(x_window_id, client)
self.adapter.client_session_changed(client, session)
# Closing the session should create a Span.
duration = 120
self.set_now(duration)
self.adapter.session_closed(session)
# Check the Span is there, and has the right name and duration.
self.assertEqual(len(self.span_storage.spans), 1)
(span,) = self.span_storage.spans
self.assertEqual(span.session, session)
self.assertEqual((span.end - span.start).total_seconds(), duration)
def test_session_renamed(self):
x_window_id = 42
client = TmuxClient(client_name='client', hostname='host')
session = TmuxSession(session_name='session', hostname='host', server_pid=42)
renamed_session = TmuxSession(session_name='renamed_session', hostname='host', server_pid=42)
# Activate a session in the adapter.
self.set_now(0)
self.adapter.set_focused_x_window_id(x_window_id, 'Terminal')
self.adapter.set_client_for_x_window_id(x_window_id, client)
self.adapter.client_session_changed(client, session)
# Renaming the session should create a Span.
duration = 120
self.set_now(duration)
self.adapter.session_renamed(client, renamed_session)
# Check the Span is there, and has the right name and duration.
self.assertEqual(len(self.span_storage.spans), 1)
(span,) = self.span_storage.spans
self.assertEqual(span.session, session)
self.assertEqual((span.end - span.start).total_seconds(), duration)
def test_client_detached(self):
x_window_id = 42
client = TmuxClient(client_name='client', hostname='host')
session = TmuxSession(session_name='session', hostname='host', server_pid=42)
# Activate a session in the adapter.
self.set_now(0)
self.adapter.set_focused_x_window_id(x_window_id, 'Terminal')
self.adapter.set_client_for_x_window_id(x_window_id, client)
self.adapter.client_session_changed(client, session)
# Detaching the client should create a Span.
duration = 120
self.set_now(duration)
self.adapter.client_detached(client)
# Check the Span is there, and has the right name and duration.
self.assertEqual(len(self.span_storage.spans), 1)
(span,) = self.span_storage.spans
self.assertEqual(span.session, session)
self.assertEqual((span.end - span.start).total_seconds(), duration)
def test_client_session_changed(self):
x_window_id = 42
client = TmuxClient(client_name='client', hostname='host')
first_session = TmuxSession(session_name='first_session', hostname='host', server_pid=42)
second_session = TmuxSession(session_name='second_session', hostname='host', server_pid=42)
# Activate a session in the adapter.
self.set_now(0)
self.adapter.set_focused_x_window_id(x_window_id, 'Terminal')
self.adapter.set_client_for_x_window_id(x_window_id, client)
self.adapter.client_session_changed(client, first_session)
# Changing the session should create a Span.
duration = 120
self.set_now(duration)
self.adapter.client_session_changed(client, second_session)
# Check the Span is there, and has the right name and duration.
self.assertEqual(len(self.span_storage.spans), 1)
(span,) = self.span_storage.spans
self.assertEqual(span.session, first_session)
self.assertEqual((span.end - span.start).total_seconds(), duration)
def test_clear_client_for_x_window_id(self):
x_window_id = 42
client = TmuxClient(client_name='client', hostname='host')
session = TmuxSession(session_name='session', hostname='host', server_pid=42)
# Activate a session in the adapter.
self.set_now(0)
self.adapter.set_focused_x_window_id(x_window_id, 'Terminal')
self.adapter.set_client_for_x_window_id(x_window_id, client)
self.adapter.client_session_changed(client, session)
# Removing tmux client ↔ X Window mapping should create a Span.
duration = 120
self.set_now(duration)
self.adapter.clear_client_for_x_window_id(x_window_id)
# Check the Span is there, and has the right name and duration.
self.assertEqual(len(self.span_storage.spans), 1)
(span,) = self.span_storage.spans
self.assertEqual(span.session, session)
self.assertEqual((span.end - span.start).total_seconds(), duration)
def test_set_focused_x_window_id(self):
x_window_id = 42
client = TmuxClient(client_name='client', hostname='host')
session = TmuxSession(session_name='session', hostname='host', server_pid=42)
# Activate a session in the adapter.
self.set_now(0)
self.adapter.set_focused_x_window_id(x_window_id, 'Terminal')
self.adapter.set_client_for_x_window_id(x_window_id, client)
self.adapter.client_session_changed(client, session)
# X Window going out of focus should create a Span.
duration = 120
self.set_now(duration)
self.adapter.set_focused_x_window_id(x_window_id - 1, 'Terminal')
# Check the Span is there, and has the right name and duration.
self.assertEqual(len(self.span_storage.spans), 1)
(span,) = self.span_storage.spans
self.assertEqual(span.session, session)
self.assertEqual((span.end - span.start).total_seconds(), duration)
if __name__ == '__main__':
unittest.main()