Skip to content

Commit 9234878

Browse files
committed
Update tests to new logic
1 parent 27e7b5b commit 9234878

13 files changed

Lines changed: 93 additions & 200 deletions

bughog/search_strategy/bgb_search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ def __init__(self, state_factory: StateFactory, considered_states: Optional[list
2727
"""
2828
super().__init__(state_factory, 0, considered_states=considered_states)
2929

30-
def next(self) -> State:
30+
def next(self, wait=True) -> State:
3131
"""
3232
Returns the next state to evaluate.
3333
"""
3434
# Fetch all evaluated states
35-
self._fetch_evaluated_states()
35+
self._fetch_evaluated_states(wait=wait)
3636

3737
if self._limit and self._limit <= len(self._considered_states):
3838
raise SequenceFinished()

bughog/search_strategy/bgb_sequence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def __init__(self, state_factory: StateFactory, limit: int, considered_states: O
2525
self._unavailability_gap_pairs: set[tuple[State, State]] = set()
2626
"""Tuples in this list are **strict** boundaries of ranges without any available binaries."""
2727

28-
def next(self) -> State:
28+
def next(self, wait=True) -> State:
2929
"""
3030
Returns the next state to evaluate.
3131
"""
3232
# Fetch all evaluated states on the first call
3333
if not self._considered_states:
34-
self._fetch_evaluated_states()
34+
self._fetch_evaluated_states(wait=wait)
3535

3636
if self._limit and self._limit <= len(self._considered_states):
3737
raise SequenceFinished()

bughog/search_strategy/composite_search.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ def __init__(self, state_factory: StateFactory, sequence_limit: int) -> None:
1111
self.sequence_strategy = BiggestGapBisectionSequence(state_factory, limit=sequence_limit)
1212
self.search_strategy: Optional[BiggestGapBisectionSearch] = None
1313

14-
def next(self) -> State:
14+
def next(self, wait=True) -> State:
1515
"""
1616
Returns the next state, based on a sequence strategy and search strategy.
1717
First, the sequence strategy decides which state to return until it returns the SequenceFinished exception.
1818
From then on, the search strategy decides which state to return.
1919
"""
2020
if self.search_strategy is None:
2121
try:
22-
return self.sequence_strategy.next()
22+
return self.sequence_strategy.next(wait=wait)
2323
except SequenceFinished:
2424
self.search_strategy = BiggestGapBisectionSearch.create_from_bgb_sequence(self.sequence_strategy)
25-
return self.search_strategy.next()
25+
return self.search_strategy.next(wait=wait)
2626
else:
27-
return self.search_strategy.next()
27+
return self.search_strategy.next(wait=wait)

bughog/search_strategy/sequence_strategy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __get_closest_available_state(self, target: State, boundaries: tuple[State,
115115
Return the closest state with an available binary.
116116
"""
117117
try:
118-
states = target.get_previous_and_next_state_with_binary()
118+
states = target.get_previous_and_next_state_with_executable()
119119
states = [state for state in states if state is not None]
120120
ordered_states = sorted(states, key=lambda x: abs(target.commit_nb - x.commit_nb))
121121

bughog/version_control/state/base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ def has_same_outcome(self, other: State) -> bool:
4040
if self.result_variables is None or other.result_variables is None:
4141
return False
4242
else:
43-
return (
44-
ExperimentResult.poc_is_reproduced(self.result_variables) ==
45-
ExperimentResult.poc_is_reproduced(other.result_variables) and
46-
ExperimentResult.poc_is_dirty(self.result_variables) ==
47-
ExperimentResult.poc_is_dirty(other.result_variables)
48-
)
43+
return ExperimentResult.poc_is_reproduced(self.result_variables) == ExperimentResult.poc_is_reproduced(other.result_variables) and ExperimentResult.poc_is_dirty(self.result_variables) == ExperimentResult.poc_is_dirty(other.result_variables)
4944

5045
@property
5146
def name(self) -> str:
@@ -131,7 +126,7 @@ def get_executable_source_urls(self) -> list[str]:
131126
"""
132127
pass
133128

134-
def get_previous_and_next_state_with_binary(self) -> tuple[State, State]:
129+
def get_previous_and_next_state_with_executable(self) -> tuple[State, State]:
135130
raise NotImplementedError(f'This function is not implemented for {self}')
136131

137132
def __repr__(self) -> str:

test/availability/__init__.py

Whitespace-only changes.

test/availability/test_folders.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

test/http_collector/test_collector.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33

44
import requests
55

6-
from bughog.evaluation.collectors.collector import Collector, Type
6+
from bughog.evaluation.collectors.collector import Collector
7+
from bughog.evaluation.collectors.requests import RequestCollector
78

89

910
class TestCollector(unittest.TestCase):
1011
@staticmethod
1112
def test_start_stop():
12-
collector = Collector([Type.REQUESTS])
13-
results = collector.collect_result_result_variables()
14-
assert results['requests'] == []
15-
assert results['req_vars'] == []
13+
collector = Collector([RequestCollector()])
14+
raw_results, variables = collector.collect_results()
15+
assert raw_results['requests'] == []
16+
assert variables == set()
1617

1718
collector.start()
1819
time.sleep(2)
1920
collector.stop()
2021

21-
results = collector.collect_result_result_variables()
22-
assert results['requests'] == []
23-
assert results['req_vars'] == []
22+
raw_results, variables = collector.collect_results()
23+
assert raw_results['requests'] == []
24+
assert variables == set()
2425
time.sleep(1)
2526
# Port should be freed
2627

@@ -30,11 +31,12 @@ def test_start_stop():
3031

3132
@staticmethod
3233
def test_requests():
33-
collector = Collector([Type.REQUESTS])
34+
collector = Collector([RequestCollector()])
3435
collector.start()
35-
response_data = {'url': 'bughog_testvar=123', 'method': 'GET', 'headers': [], 'content': 'test'}
36+
response_data = {'url': 'https://leak.test/report/?bughog_testvar=123', 'method': 'GET', 'headers': [], 'content': 'test'}
3637
requests.post('http://localhost:5001', json=response_data)
3738
time.sleep(1)
3839
collector.stop()
39-
results = collector.collect_result_result_variables()
40-
assert results['requests'] == [response_data]
40+
raw_results, variables = collector.collect_results()
41+
assert raw_results['requests'] == [response_data]
42+
assert len(variables) == 1 and ('testvar', '123') in variables

test/sequence/test_biggest_gap_bisection_search.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ def test_sbg_search_always_available_search(self):
1212
helper.always_has_binary,
1313
outcome_func=lambda x: True if x < 50 else False)
1414
sequence = BiggestGapBisectionSearch(state_factory)
15-
index_sequence = [sequence.next().index for _ in range(8)]
15+
index_sequence = [sequence.next(wait=False).index for _ in range(8)]
1616
assert index_sequence == [0, 99, 49, 74, 61, 55, 52, 50]
17-
self.assertRaises(SequenceFinished, sequence.next)
17+
self.assertRaises(SequenceFinished, lambda: sequence.next(wait=False))
1818

1919
def test_sbg_search_even_available_search(self):
2020
state_factory = helper.create_state_factory(
2121
helper.only_has_binaries_for_even,
2222
outcome_func=lambda x: True if x < 35 else False)
2323
sequence = BiggestGapBisectionSearch(state_factory)
2424

25-
assert sequence.next().index == 0
26-
assert [state.index for state in sequence._completed_states] == [0]
25+
assert sequence.next(wait=False).index == 0
26+
assert [state.index for state in sequence._considered_states] == [0]
2727
assert sequence._unavailability_gap_pairs == set()
2828

2929
while True:
3030
try:
31-
sequence.next()
31+
sequence.next(wait=False)
3232
except SequenceFinished:
3333
break
3434

35-
assert ([state.index for state in sequence._completed_states]
35+
assert ([state.index for state in sequence._considered_states]
3636
== [0, 24, 30, 32, 34, 36, 48, 98])
3737

38-
self.assertRaises(SequenceFinished, sequence.next)
38+
self.assertRaises(SequenceFinished, lambda: sequence.next(wait=False))
3939
assert {(first.index, last.index) for (first, last) in sequence._unavailability_gap_pairs} == {(34, 36)}
4040

4141

@@ -45,23 +45,23 @@ def test_sbg_search_few_available_search(self):
4545
outcome_func=lambda x: True if x < 35 else False)
4646
sequence = BiggestGapBisectionSearch(state_factory)
4747

48-
assert sequence.next().index == 0
49-
assert [state.index for state in sequence._completed_states] == [0]
48+
assert sequence.next(wait=False).index == 0
49+
assert [state.index for state in sequence._considered_states] == [0]
5050
assert sequence._unavailability_gap_pairs == set()
5151

52-
assert sequence.next().index == 99
53-
assert [state.index for state in sequence._completed_states] == [0, 99]
52+
assert sequence.next(wait=False).index == 99
53+
assert [state.index for state in sequence._considered_states] == [0, 99]
5454

55-
assert sequence.next().index == 44
56-
assert [state.index for state in sequence._completed_states] == [0, 44, 99]
55+
assert sequence.next(wait=False).index == 44
56+
assert [state.index for state in sequence._considered_states] == [0, 44, 99]
5757

58-
assert sequence.next().index == 22
59-
assert [state.index for state in sequence._completed_states] == [0, 22, 44, 99]
58+
assert sequence.next(wait=False).index == 22
59+
assert [state.index for state in sequence._considered_states] == [0, 22, 44, 99]
6060

61-
assert sequence.next().index == 33
62-
assert [state.index for state in sequence._completed_states] == [0, 22, 33, 44, 99]
61+
assert sequence.next(wait=False).index == 33
62+
assert [state.index for state in sequence._considered_states] == [0, 22, 33, 44, 99]
6363

64-
self.assertRaises(SequenceFinished, sequence.next)
64+
self.assertRaises(SequenceFinished, lambda: sequence.next(wait=False))
6565
assert {(first.index, last.index) for (first, last) in sequence._unavailability_gap_pairs} == {(33, 44)}
6666

6767
def test_sbg_search_few_available_search_complex(self):
@@ -73,9 +73,9 @@ def test_sbg_search_few_available_search_complex(self):
7373

7474
while True:
7575
try:
76-
sequence.next()
76+
sequence.next(wait=False)
7777
except SequenceFinished:
7878
break
7979

80-
assert ([state.index for state in sequence._completed_states]
80+
assert ([state.index for state in sequence._considered_states]
8181
== [0, 12, 22, 34, 36, 38, 44, 56, 66, 68, 72, 78, 88, 98])

test/sequence/test_biggest_gap_bisection_sequence.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ class TestBiggestGapBisectionSequence(unittest.TestCase):
1010
def test_sbg_sequence_always_available(self):
1111
state_factory = helper.create_state_factory(helper.always_has_binary)
1212
sequence = BiggestGapBisectionSequence(state_factory, 12)
13-
index_sequence = [sequence.next().index for _ in range(12)]
13+
index_sequence = [sequence.next(wait=False).index for _ in range(12)]
1414
assert index_sequence == [0, 99, 49, 74, 24, 36, 61, 86, 12, 42, 67, 92]
15-
self.assertRaises(SequenceFinished, sequence.next)
15+
self.assertRaises(SequenceFinished, lambda: sequence.next(wait=False))
1616

1717
def test_sbg_sequence_even_available(self):
1818
state_factory = helper.create_state_factory(helper.only_has_binaries_for_even)
1919
sequence = BiggestGapBisectionSequence(state_factory, 12)
20-
index_sequence = [sequence.next().index for _ in range(12)]
20+
index_sequence = [sequence.next(wait=False).index for _ in range(12)]
2121
assert index_sequence == [0, 98, 48, 72, 24, 84, 12, 36, 60, 90, 6, 18]
2222

2323
def test_sbg_sequence_almost_none_available(self):
2424
state_factory = helper.create_state_factory(helper.has_very_few_binaries)
2525
sequence = BiggestGapBisectionSequence(state_factory, 10)
26-
index_sequence = [sequence.next().index for _ in range(10)]
26+
index_sequence = [sequence.next(wait=False).index for _ in range(10)]
2727
assert index_sequence == [0, 99, 44, 66, 22, 77, 11, 33, 55, 88]
28-
self.assertRaises(SequenceFinished, sequence.next)
28+
self.assertRaises(SequenceFinished, lambda: sequence.next(wait=False))
2929

3030
def test_sbg_sequence_sparse_first_half_avaiable(self):
3131
state_factory = helper.create_state_factory(helper.has_very_few_binaries_in_first_half)
3232
sequence = BiggestGapBisectionSequence(state_factory, 17)
33-
index_sequence = [sequence.next().index for _ in range(17)]
33+
index_sequence = [sequence.next(wait=False).index for _ in range(17)]
3434
assert index_sequence == [0, 99, 50, 22, 74, 44, 86, 62, 92, 56, 68, 80, 95, 53, 59, 65, 71]
3535

3636
def test_sbg_sequence_always_available_with_evaluated_states(self):
3737
state_factory = helper.create_state_factory(helper.always_has_binary, evaluated_indexes=[49, 61])
3838
sequence = BiggestGapBisectionSequence(state_factory, 17)
39-
index_sequence = [sequence.next().index for _ in range(15)]
39+
index_sequence = [sequence.next(wait=False).index for _ in range(15)]
4040
print(index_sequence)
4141
assert index_sequence == [0, 99, 24, 80, 36, 12, 70, 89, 42, 6, 18, 30, 55, 75, 94]
42-
self.assertRaises(SequenceFinished, sequence.next)
42+
self.assertRaises(SequenceFinished, lambda: sequence.next(wait=False))

0 commit comments

Comments
 (0)