1-
2- from six import string_types
1+ import time
32from types import GeneratorType
43from collections import Iterable
54
6- from scrapinghub . client import Frontiers , Frontier , FrontierSlot
5+ from six import string_types
76
7+ from scrapinghub .client import Frontiers , Frontier , FrontierSlot
88from .conftest import TEST_FRONTIER_NAME , TEST_FRONTIER_SLOT
99
1010
1111def _add_test_requests_to_frontier (frontier ):
1212 slot = frontier .get (TEST_FRONTIER_SLOT )
13- slot .add ([{'fp' : '/some/path.html' }, {'fp' : '/other/path.html' }])
13+ slot .q . add ([{'fp' : '/some/path.html' }, {'fp' : '/other/path.html' }])
1414 slot .flush ()
1515
1616
1717def test_frontiers (project , frontier ):
1818 # reset a test slot and add some requests to init it
19- frontier .delete (TEST_FRONTIER_SLOT )
19+ frontier .get (TEST_FRONTIER_SLOT ). delete ( )
2020 _add_test_requests_to_frontier (frontier )
2121
2222 assert isinstance (project .frontiers , Frontiers )
@@ -42,7 +42,7 @@ def test_frontiers(project, frontier):
4242
4343def test_frontier (project , frontier ):
4444 # add some requests to test frontier to init a test slot
45- frontier .delete (TEST_FRONTIER_SLOT )
45+ frontier .get (TEST_FRONTIER_SLOT ). delete ( )
4646 _add_test_requests_to_frontier (frontier )
4747
4848 slots = frontier .iter ()
@@ -60,13 +60,13 @@ def test_frontier(project, frontier):
6060
6161def test_frontier_slot (project , frontier ):
6262 # add some requests to test frontier to init a test slot
63- frontier .delete (TEST_FRONTIER_SLOT )
63+ frontier .get (TEST_FRONTIER_SLOT ). delete ( )
6464 _add_test_requests_to_frontier (frontier )
6565
6666 slot = frontier .get (TEST_FRONTIER_SLOT )
6767
6868 # get all batches from slot and validate its content
69- batches_iter = slot .iter ()
69+ batches_iter = slot .q . iter ()
7070 assert isinstance (batches_iter , GeneratorType )
7171 batches = list (batches_iter )
7272 assert len (batches ) == 1
@@ -78,21 +78,53 @@ def test_frontier_slot(project, frontier):
7878 assert requests == [['/some/path.html' , None ],
7979 ['/other/path.html' , None ]]
8080
81- # validate that slot.list() returns same data as slot.iter()
82- batches_list = slot .list ()
81+ # validate that slot.list() returns same data as slot.q. iter()
82+ batches_list = slot .q . list ()
8383 assert isinstance (batches , list )
8484 assert batches_list == batches
8585
8686 # add a requests with additional parameters
87- slot .add ([{'fp' : 'page1.html' , 'p' : 1 , 'qdata' : {'depth' : 1 }}])
87+ slot .q . add ([{'fp' : 'page1.html' , 'p' : 1 , 'qdata' : {'depth' : 1 }}])
8888 slot .flush ()
89- batches = slot .list ()
89+ batches = slot .q . list ()
9090 assert len (batches ) == 2
9191 assert batches [1 ]['requests' ] == [['page1.html' , {'depth' : 1 }]]
9292
9393 # drop all batches and validate that slot is empty
94- slot .delete ([batch ['id' ] for batch in batches ])
95- assert slot .list () == []
94+ slot .q . delete ([batch ['id' ] for batch in batches ])
95+ assert slot .q . list () == []
9696
9797 slot .delete ()
9898 assert TEST_FRONTIER_SLOT not in frontier .list ()
99+
100+
101+ def test_frontier_newcount (project ):
102+ # add some requests to test frontier to init a test slot
103+ frontier = project .frontiers .get (TEST_FRONTIER_NAME )
104+ first_slot = frontier .get (TEST_FRONTIER_SLOT )
105+ first_slot .delete ()
106+
107+ assert frontier ._frontiers .newcount == 0
108+ assert frontier .newcount == 0
109+ assert first_slot .newcount == 0
110+
111+ # shorter batch interval for faster tests
112+ frontier ._frontiers ._origin .batch_interval = 0.1
113+ _add_test_requests_to_frontier (frontier )
114+ time .sleep (0.5 )
115+
116+ assert frontier ._frontiers .newcount == 2
117+ assert frontier .newcount == 2
118+ assert first_slot .newcount == 2
119+
120+ second_slot = frontier .get ('test2.com' )
121+ second_slot .delete ()
122+ second_slot .q .add ([{'fp' : '/different_path.html' }])
123+ second_slot .flush ()
124+
125+ assert frontier ._frontiers .newcount == 3
126+ assert frontier .newcount == 3
127+ assert second_slot .newcount == 1
128+ assert first_slot .newcount == 2
129+
130+ frontier ._frontiers .close ()
0 commit comments