Merge lp://staging/~al-maisan/launchpad/spurious-tests-525329 into lp://staging/launchpad/db-devel

Proposed by Muharem Hrnjadovic
Status: Merged
Approved by: Graham Binns
Approved revision: no longer in the source branch.
Merged at revision: not available
Proposed branch: lp://staging/~al-maisan/launchpad/spurious-tests-525329
Merge into: lp://staging/launchpad/db-devel
Diff against target: 123 lines (+34/-12)
2 files modified
lib/lp/soyuz/model/buildqueue.py (+10/-4)
lib/lp/soyuz/tests/test_buildqueue.py (+24/-8)
To merge this branch: bzr merge lp://staging/~al-maisan/launchpad/spurious-tests-525329
Reviewer Review Type Date Requested Status
Graham Binns (community) code Approve
Review via email: mp+20233@code.staging.launchpad.net
To post a comment you must log in.
Revision history for this message
Muharem Hrnjadovic (al-maisan) wrote :

Hello,

quite a few tests that predict times or durations failed in spurious
fashion in the last few days.

The branch at hand introduces a BuildQueue._now() method that is used by the
BuildQueue code under test while enabling the unit tests to "monkey-patch"
that method and have it return a constant time in order to facilitate the
testing process.

This is based on an idea of Julian's.

Tests to run:

    bin/test -vv -t test_buildqueue

No "make lint" errors or warnings.

Revision history for this message
Graham Binns (gmb) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/soyuz/model/buildqueue.py'
2--- lib/lp/soyuz/model/buildqueue.py 2010-02-22 08:30:06 +0000
3+++ lib/lp/soyuz/model/buildqueue.py 2010-03-01 16:30:47 +0000
4@@ -223,16 +223,17 @@
5
6 head_job_processor, head_job_virtualized = head_job_platform
7
8+ now = self._now()
9 delay_query = """
10 SELECT MIN(
11 CASE WHEN
12 EXTRACT(EPOCH FROM
13 (BuildQueue.estimated_duration -
14- (((now() AT TIME ZONE 'UTC') - Job.date_started)))) >= 0
15+ (((%s AT TIME ZONE 'UTC') - Job.date_started)))) >= 0
16 THEN
17 EXTRACT(EPOCH FROM
18 (BuildQueue.estimated_duration -
19- (((now() AT TIME ZONE 'UTC') - Job.date_started))))
20+ (((%s AT TIME ZONE 'UTC') - Job.date_started))))
21 ELSE
22 -- Assume that jobs that have overdrawn their estimated
23 -- duration time budget will complete within 2 minutes.
24@@ -253,7 +254,7 @@
25 AND Job.status = %s
26 AND Builder.virtualized = %s
27 """ % sqlvalues(
28- JobStatus.RUNNING,
29+ now, now, JobStatus.RUNNING,
30 normalize_virtualization(head_job_virtualized))
31
32 if head_job_processor is not None:
33@@ -460,10 +461,15 @@
34
35 # A job will not get dispatched in less than 5 seconds no matter what.
36 start_time = max(5, min_wait_time + sum_of_delays)
37- result = datetime.utcnow() + timedelta(seconds=start_time)
38+ result = self._now() + timedelta(seconds=start_time)
39
40 return result
41
42+ @staticmethod
43+ def _now():
44+ """Provide utcnow() while allowing test code to monkey-patch this."""
45+ return datetime.utcnow()
46+
47
48 class BuildQueueSet(object):
49 """Utility to deal with BuildQueue content class."""
50
51=== modified file 'lib/lp/soyuz/tests/test_buildqueue.py'
52--- lib/lp/soyuz/tests/test_buildqueue.py 2010-02-25 16:34:13 +0000
53+++ lib/lp/soyuz/tests/test_buildqueue.py 2010-03-01 16:30:47 +0000
54@@ -96,8 +96,12 @@
55 queue_entry.lastscore)
56
57
58-def check_mintime_to_builder(test, bq, min_time):
59+def check_mintime_to_builder(
60+ test, bq, min_time, time_stamp=datetime.utcnow()):
61 """Test the estimated time until a builder becomes available."""
62+ # Monkey-patch BuildQueueSet._now() so it returns a constant time stamp
63+ # that's not too far in the future. This avoids spurious test failures.
64+ monkey_patch_the_now_property(bq)
65 delay = bq._estimateTimeToNextBuilder()
66 test.assertTrue(
67 delay <= min_time,
68@@ -131,8 +135,24 @@
69 return builder_data[(getattr(job.processor, 'id', None), job.virtualized)]
70
71
72+def monkey_patch_the_now_property(buildqueue):
73+ """Patch BuildQueue._now() so it returns a constant time stamp.
74+
75+ This avoids spurious test failures.
76+ """
77+ # Use the date/time the job started if available.
78+ time_stamp = buildqueue.job.date_started
79+ if not time_stamp:
80+ time_stamp = datetime.utcnow()
81+ buildqueue._now = lambda: time_stamp
82+ return time_stamp
83+
84+
85 def check_estimate(test, job, delay_in_seconds):
86 """Does the dispatch time estimate match the expectation?"""
87+ # Monkey-patch BuildQueueSet._now() so it returns a constant time stamp.
88+ # This avoids spurious test failures.
89+ time_stamp = monkey_patch_the_now_property(job)
90 estimate = job.getEstimatedJobStartTime()
91 if delay_in_seconds is None:
92 test.assertEquals(
93@@ -140,7 +160,7 @@
94 "An estimate should not be possible at present but one was "
95 "returned (%s) nevertheless." % estimate)
96 else:
97- estimate -= datetime.utcnow()
98+ estimate -= time_stamp
99 test.assertTrue(
100 estimate.seconds <= delay_in_seconds,
101 "The estimated delay deviates from the expected one (%s > %s)" %
102@@ -495,9 +515,7 @@
103 class TestMinTimeToNextBuilder(SingleArchBuildsBase):
104 """Test estimated time-to-builder with builds targetting a single
105 processor."""
106- # XXX Michael Nelson 20100223 bug=525329
107- # This is still failing spuriously.
108- def disabled_test_min_time_to_next_builder(self):
109+ def test_min_time_to_next_builder(self):
110 """When is the next builder capable of running the job at the head of
111 the queue becoming available?"""
112 # Test the estimation of the minimum time until a builder becomes
113@@ -684,9 +702,7 @@
114
115 class TestMinTimeToNextBuilderMulti(MultiArchBuildsBase):
116 """Test estimated time-to-builder with builds and multiple processors."""
117- # XXX Michael Nelson 20100223 bug=525329
118- # This is still failing spuriously.
119- def disabled_test_min_time_to_next_builder(self):
120+ def test_min_time_to_next_builder(self):
121 """When is the next builder capable of running the job at the head of
122 the queue becoming available?"""
123 # One of four builders for the 'apg' build is immediately available.

Subscribers

People subscribed via source and target branches

to status/vote changes: