forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parallel_scraping.py
More file actions
347 lines (282 loc) · 11.3 KB
/
test_parallel_scraping.py
File metadata and controls
347 lines (282 loc) · 11.3 KB
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
#!/usr/bin/env python3
"""
Tests for parallel scraping, unlimited mode, and rate limiting features (PR #144)
"""
import os
import tempfile
import unittest
from skill_seekers.cli.doc_scraper import DocToSkillConverter
class TestParallelScrapingConfiguration(unittest.TestCase):
"""Test parallel scraping configuration and initialization"""
def setUp(self):
"""Save original working directory"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Restore original working directory"""
os.chdir(self.original_cwd)
def test_single_worker_default(self):
"""Test default is single-worker mode"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": 10,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.workers, 1)
self.assertFalse(hasattr(converter, "lock"))
def test_multiple_workers_creates_lock(self):
"""Test multiple workers creates thread lock"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": 10,
"workers": 4,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.workers, 4)
self.assertTrue(hasattr(converter, "lock"))
def test_workers_from_config(self):
"""Test workers parameter is read from config"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"workers": 8,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.workers, 8)
class TestUnlimitedMode(unittest.TestCase):
"""Test unlimited scraping mode"""
def setUp(self):
"""Save original working directory"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Restore original working directory"""
os.chdir(self.original_cwd)
def test_unlimited_with_none(self):
"""Test max_pages: None enables unlimited mode"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": None,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertIsNone(converter.config.get("max_pages"))
def test_unlimited_with_minus_one(self):
"""Test max_pages: -1 enables unlimited mode"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": -1,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.config.get("max_pages"), -1)
def test_limited_mode_default(self):
"""Test default max_pages is limited"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
max_pages = converter.config.get("max_pages", 500)
self.assertIsNotNone(max_pages)
self.assertGreater(max_pages, 0)
class TestRateLimiting(unittest.TestCase):
"""Test rate limiting configuration"""
def setUp(self):
"""Save original working directory"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Restore original working directory"""
os.chdir(self.original_cwd)
def test_rate_limit_from_config(self):
"""Test rate_limit is read from config"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"rate_limit": 0.1,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.config.get("rate_limit"), 0.1)
def test_rate_limit_default(self):
"""Test default rate_limit is 0.5"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.config.get("rate_limit", 0.5), 0.5)
def test_zero_rate_limit_disables(self):
"""Test rate_limit: 0 disables rate limiting"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"rate_limit": 0,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.config.get("rate_limit"), 0)
class TestThreadSafety(unittest.TestCase):
"""Test thread-safety fixes"""
def setUp(self):
"""Save original working directory"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Restore original working directory"""
os.chdir(self.original_cwd)
def test_lock_protects_visited_urls(self):
"""Test visited_urls operations are protected by lock"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"workers": 4,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
# Verify lock exists
self.assertTrue(hasattr(converter, "lock"))
# Verify it's a threading.Lock
import threading
self.assertIsInstance(converter.lock, type(threading.Lock()))
def test_single_worker_no_lock(self):
"""Test single worker doesn't create unnecessary lock"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"workers": 1,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(hasattr(converter, "lock"))
class TestScrapingModes(unittest.TestCase):
"""Test different scraping mode combinations"""
def setUp(self):
"""Save original working directory"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Restore original working directory"""
os.chdir(self.original_cwd)
def test_single_threaded_limited(self):
"""Test traditional single-threaded limited mode"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": 10,
"workers": 1,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.workers, 1)
self.assertEqual(converter.config.get("max_pages"), 10)
def test_parallel_limited(self):
"""Test parallel scraping with page limit"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": 100,
"workers": 4,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.workers, 4)
self.assertEqual(converter.config.get("max_pages"), 100)
self.assertTrue(hasattr(converter, "lock"))
def test_parallel_unlimited(self):
"""Test parallel scraping with unlimited pages"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": None,
"workers": 8,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.workers, 8)
self.assertIsNone(converter.config.get("max_pages"))
self.assertTrue(hasattr(converter, "lock"))
def test_fast_scraping_mode(self):
"""Test fast scraping with low rate limit and workers"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"rate_limit": 0.1,
"workers": 8,
"max_pages": 1000,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertEqual(converter.workers, 8)
self.assertEqual(converter.config.get("rate_limit"), 0.1)
class TestDryRunWithNewFeatures(unittest.TestCase):
"""Test dry-run mode works with new features"""
def setUp(self):
"""Save original working directory"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Restore original working directory"""
os.chdir(self.original_cwd)
def test_dry_run_with_parallel(self):
"""Test dry-run with parallel workers"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"workers": 4,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(converter.dry_run)
self.assertEqual(converter.workers, 4)
def test_dry_run_with_unlimited(self):
"""Test dry-run with unlimited mode"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": None,
}
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(converter.dry_run)
self.assertIsNone(converter.config.get("max_pages"))
if __name__ == "__main__":
unittest.main()