Skip to content

Commit

Permalink
Merge pull request buildbot#8202 from tdesveaux/inlineCB-to-async/tes…
Browse files Browse the repository at this point in the history
…t/fake

test: convert Twisted's inlineCallbacks to async in fake
  • Loading branch information
p12tic authored Nov 9, 2024
2 parents 9e8527a + 657d2bb commit 7da70da
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 125 deletions.
12 changes: 8 additions & 4 deletions master/buildbot/test/fake/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from twisted.internet import defer

from buildbot.util.twisted import async_to_deferred


class FakeConnection:
is_fake_test_connection = True
Expand All @@ -30,19 +32,21 @@ def __init__(self, testcase, name, step, commands_numbers_to_interrupt):
self._next_command_number = 0
self._blocked_deferreds = []

@defer.inlineCallbacks
def remoteStartCommand(self, remote_command, builder_name, command_id, command_name, args):
@async_to_deferred
async def remoteStartCommand(
self, remote_command, builder_name, command_id, command_name, args
) -> None:
self._waiting_for_interrupt = False
if self._next_command_number in self._commands_numbers_to_interrupt:
self._waiting_for_interrupt = True

yield self.step.interrupt('interrupt reason')
await self.step.interrupt('interrupt reason')

if self._waiting_for_interrupt:
raise RuntimeError("Interrupted step, but command was not interrupted")

self._next_command_number += 1
yield self.testcase._connection_remote_start_command(remote_command, self, builder_name)
await self.testcase._connection_remote_start_command(remote_command, self, builder_name)

# running behaviors may still attempt interrupt the command
if self._waiting_for_interrupt:
Expand Down
73 changes: 40 additions & 33 deletions master/buildbot/test/fake/fakedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#
# Copyright Buildbot Team Members

from __future__ import annotations

import json

from twisted.internet import defer
Expand All @@ -23,6 +25,7 @@
from buildbot.db.buildrequests import AlreadyClaimedError
from buildbot.test.util import validation
from buildbot.util import service
from buildbot.util.twisted import async_to_deferred


class FakeUpdates(service.AsyncService):
Expand Down Expand Up @@ -149,8 +152,8 @@ def masterStopped(self, name, masterid):
def expireMasters(self, forceHouseKeeping=False):
return defer.succeed(None)

@defer.inlineCallbacks
def addBuildset(
@async_to_deferred
async def addBuildset(
self,
waited_for,
scheduler=None,
Expand All @@ -163,7 +166,7 @@ def addBuildset(
parent_buildid=None,
parent_relationship=None,
priority=0,
):
) -> tuple[int, dict[int, int]]:
if sourcestamps is None:
sourcestamps = []
if properties is None:
Expand All @@ -187,7 +190,7 @@ def addBuildset(

# call through to the db layer, since many scheduler tests expect to
# find the buildset in the db later - TODO fix this!
bsid, brids = yield self.master.db.buildsets.addBuildset(
bsid, brids = await self.master.db.buildsets.addBuildset(
sourcestamps=sourcestamps,
reason=reason,
properties=properties,
Expand All @@ -204,8 +207,8 @@ def maybeBuildsetComplete(self, bsid):
self.maybeBuildsetCompleteCalls += 1
return defer.succeed(None)

@defer.inlineCallbacks
def claimBuildRequests(self, brids, claimed_at=None):
@async_to_deferred
async def claimBuildRequests(self, brids, claimed_at=None) -> bool:
validation.verifyType(
self.testcase, 'brids', brids, validation.ListValidator(validation.IntValidator())
)
Expand All @@ -218,22 +221,22 @@ def claimBuildRequests(self, brids, claimed_at=None):
if not brids:
return True
try:
yield self.master.db.buildrequests.claimBuildRequests(
await self.master.db.buildrequests.claimBuildRequests(
brids=brids, claimed_at=claimed_at
)
except AlreadyClaimedError:
return False
self.claimedBuildRequests.update(set(brids))
return True

@defer.inlineCallbacks
def unclaimBuildRequests(self, brids):
@async_to_deferred
async def unclaimBuildRequests(self, brids) -> None:
validation.verifyType(
self.testcase, 'brids', brids, validation.ListValidator(validation.IntValidator())
)
self.claimedBuildRequests.difference_update(set(brids))
if brids:
yield self.master.db.buildrequests.unclaimBuildRequests(brids)
await self.master.db.buildrequests.unclaimBuildRequests(brids)

def completeBuildRequests(self, brids, results, complete_at=None):
validation.verifyType(
Expand All @@ -251,16 +254,16 @@ def completeBuildRequests(self, brids, results, complete_at=None):
def rebuildBuildrequest(self, buildrequest):
return defer.succeed(None)

@defer.inlineCallbacks
def update_project_info(
@async_to_deferred
async def update_project_info(
self,
projectid,
slug,
description,
description_format,
description_html,
):
yield self.master.db.projects.update_project_info(
) -> None:
await self.master.db.projects.update_project_info(
projectid, slug, description, description_format, description_html
)

Expand All @@ -275,11 +278,11 @@ def updateBuilderList(self, masterid, builderNames):
self.builderNames = builderNames
return defer.succeed(None)

@defer.inlineCallbacks
def updateBuilderInfo(
@async_to_deferred
async def updateBuilderInfo(
self, builderid, description, description_format, description_html, projectid, tags
):
yield self.master.db.builders.updateBuilderInfo(
) -> None:
await self.master.db.builders.updateBuilderInfo(
builderid, description, description_format, description_html, projectid, tags
)

Expand Down Expand Up @@ -364,11 +367,11 @@ def setBuildProperty(self, buildid, name, value, source):
validation.verifyType(self.testcase, 'source', source, validation.StringValidator())
return defer.succeed(None)

@defer.inlineCallbacks
def setBuildProperties(self, buildid, properties):
@async_to_deferred
async def setBuildProperties(self, buildid, properties) -> None:
for k, v, s in properties.getProperties().asList():
self.properties.append((buildid, k, v, s))
yield self.setBuildProperty(buildid, k, v, s)
await self.setBuildProperty(buildid, k, v, s)

def addStep(self, buildid, name):
validation.verifyType(self.testcase, 'buildid', buildid, validation.IntValidator())
Expand Down Expand Up @@ -477,17 +480,19 @@ def set_worker_graceful(self, workerid, graceful):
return self.master.db.workers.set_worker_graceful(workerid, graceful)

# methods form BuildData resource
@defer.inlineCallbacks
def setBuildData(self, buildid, name, value, source):
@async_to_deferred
async def setBuildData(self, buildid, name, value, source) -> None:
validation.verifyType(self.testcase, 'buildid', buildid, validation.IntValidator())
validation.verifyType(self.testcase, 'name', name, validation.StringValidator())
validation.verifyType(self.testcase, 'value', value, validation.BinaryValidator())
validation.verifyType(self.testcase, 'source', source, validation.StringValidator())
yield self.master.db.build_data.setBuildData(buildid, name, value, source)
await self.master.db.build_data.setBuildData(buildid, name, value, source)

# methods from TestResultSet resource
@defer.inlineCallbacks
def addTestResultSet(self, builderid, buildid, stepid, description, category, value_unit):
@async_to_deferred
async def addTestResultSet(
self, builderid, buildid, stepid, description, category, value_unit
) -> int:
validation.verifyType(self.testcase, 'builderid', builderid, validation.IntValidator())
validation.verifyType(self.testcase, 'buildid', buildid, validation.IntValidator())
validation.verifyType(self.testcase, 'stepid', stepid, validation.IntValidator())
Expand All @@ -497,13 +502,15 @@ def addTestResultSet(self, builderid, buildid, stepid, description, category, va
validation.verifyType(self.testcase, 'category', category, validation.StringValidator())
validation.verifyType(self.testcase, 'value_unit', value_unit, validation.StringValidator())

test_result_setid = yield self.master.db.test_result_sets.addTestResultSet(
test_result_setid = await self.master.db.test_result_sets.addTestResultSet(
builderid, buildid, stepid, description, category, value_unit
)
return test_result_setid

@defer.inlineCallbacks
def completeTestResultSet(self, test_result_setid, tests_passed=None, tests_failed=None):
@async_to_deferred
async def completeTestResultSet(
self, test_result_setid, tests_passed=None, tests_failed=None
) -> None:
validation.verifyType(
self.testcase, 'test_result_setid', test_result_setid, validation.IntValidator()
)
Expand All @@ -520,14 +527,14 @@ def completeTestResultSet(self, test_result_setid, tests_passed=None, tests_fail
validation.NoneOk(validation.IntValidator()),
)

yield self.master.db.test_result_sets.completeTestResultSet(
await self.master.db.test_result_sets.completeTestResultSet(
test_result_setid, tests_passed, tests_failed
)

# methods from TestResult resource
@defer.inlineCallbacks
def addTestResults(self, builderid, test_result_setid, result_values):
yield self.master.db.test_results.addTestResults(
@async_to_deferred
async def addTestResults(self, builderid, test_result_setid, result_values) -> None:
await self.master.db.test_results.addTestResults(
builderid, test_result_setid, result_values
)

Expand Down
17 changes: 9 additions & 8 deletions master/buildbot/test/fake/fakemaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from buildbot.test.fake.machine import FakeMachineManager
from buildbot.test.fake.secrets import FakeSecretStorage
from buildbot.util import service
from buildbot.util.twisted import async_to_deferred


class FakeCache:
Expand Down Expand Up @@ -137,8 +138,8 @@ def subscribeToBuildRequests(self, callback):
# Leave this alias, in case we want to add more behavior later


@defer.inlineCallbacks
def make_master(
@async_to_deferred
async def make_master(
testcase,
wantMq=False,
wantDb=False,
Expand All @@ -148,7 +149,7 @@ def make_master(
with_secrets: dict | None = None,
url=None,
**kwargs,
):
) -> FakeMaster:
if wantRealReactor:
_reactor = reactor
else:
Expand All @@ -164,20 +165,20 @@ def make_master(
if wantMq:
assert testcase is not None, "need testcase for wantMq"
master.mq = fakemq.FakeMQConnector(testcase)
yield master.mq.setServiceParent(master)
await master.mq.setServiceParent(master)
if wantDb:
assert testcase is not None, "need testcase for wantDb"
master.db = fakedb.FakeDBConnector(master.basedir, testcase)
master.db.configured_url = 'sqlite://'
yield master.db.setServiceParent(master)
yield master.db.setup()
await master.db.setServiceParent(master)
await master.db.setup()
testcase.addCleanup(master.db._shutdown)

if wantData:
master.data = fakedata.FakeDataConnector(master, testcase)
if wantGraphql:
master.graphql = GraphQLConnector()
yield master.graphql.setServiceParent(master)
await master.graphql.setServiceParent(master)
master.graphql.data = master.data.realConnector
master.data._scanModule(endpoint)
master.config.www = {'graphql': {"debug": True}}
Expand All @@ -189,6 +190,6 @@ def make_master(
secret_service = SecretManager()
secret_service.services = [FakeSecretStorage(secretdict=with_secrets)]
# This should be awaited, but no other call to `setServiceParent` are awaited here
yield secret_service.setServiceParent(master)
await secret_service.setServiceParent(master)

return master
15 changes: 8 additions & 7 deletions master/buildbot/test/fake/fakemq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from buildbot.util import deferwaiter
from buildbot.util import service
from buildbot.util import tuplematch
from buildbot.util.twisted import async_to_deferred


class FakeMQConnector(service.AsyncMultiService, base.MQBase):
Expand All @@ -39,10 +40,10 @@ def __init__(self, testcase):
self.qrefs = []
self._deferwaiter = deferwaiter.DeferWaiter()

@defer.inlineCallbacks
def stopService(self):
yield self._deferwaiter.wait()
yield super().stopService()
@async_to_deferred
async def stopService(self) -> None:
await self._deferwaiter.wait()
await super().stopService()

def setup(self):
self.setup_called = True
Expand Down Expand Up @@ -102,10 +103,10 @@ def assertProductions(self, exp, orderMatters=True):
self.testcase.assertEqual(sorted(self.productions), sorted(exp))
self.productions = []

@defer.inlineCallbacks
def wait_consumed(self):
@async_to_deferred
async def wait_consumed(self) -> None:
# waits until all messages have been consumed
yield self._deferwaiter.wait()
await self._deferwaiter.wait()


class FakeQueueRef:
Expand Down
15 changes: 8 additions & 7 deletions master/buildbot/test/fake/httpclientservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from buildbot.util import service
from buildbot.util import toJson
from buildbot.util import unicode2bytes
from buildbot.util.twisted import async_to_deferred

log = Logger()

Expand Down Expand Up @@ -89,8 +90,8 @@ def updateHeaders(self, headers):
self._session.update_headers(headers)

@classmethod
@defer.inlineCallbacks
def getService(cls, master, case, *args, **kwargs):
@async_to_deferred
async def getService(cls, master, case, *args, **kwargs):
def assertNotCalled(self, *_args, **_kwargs):
case.fail(
f"HTTPClientService called with *{_args!r}, **{_kwargs!r} "
Expand All @@ -99,7 +100,7 @@ def assertNotCalled(self, *_args, **_kwargs):

case.patch(httpclientservice.HTTPClientService, "__init__", assertNotCalled)

service = yield super().getService(master, *args, **kwargs)
service = await super().getService(master, *args, **kwargs)
service.case = case
case.addCleanup(service.assertNoOutstanding)

Expand Down Expand Up @@ -152,8 +153,8 @@ def assertNoOutstanding(self):
0, len(self._expected), f"expected more http requests:\n {self._expected!r}"
)

@defer.inlineCallbacks
def _do_request(
@async_to_deferred
async def _do_request(
self,
session,
method,
Expand All @@ -170,7 +171,7 @@ def _do_request(
cert=None,
allow_redirects=None, # checks are not implemented
proxies=None, # checks are not implemented
):
) -> IHttpResponse:
if ep.startswith('http://') or ep.startswith('https://'):
pass
else:
Expand Down Expand Up @@ -262,7 +263,7 @@ def _do_request(
)

if processing_delay_s is not None:
yield util.asyncSleep(1, reactor=self.master.reactor)
await util.asyncSleep(1, reactor=self.master.reactor)

return ResponseWrapper(expect['code'], expect['content'])

Expand Down
Loading

0 comments on commit 7da70da

Please sign in to comment.