mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-04-13 10:09:49 +08:00
Send module build state change message after commit to database
In MBS, there are two cases to send a message when a module build moves to a new state. One is to create a new module build, with ModuleBuild.create particularly, when user submit a module build. Another one is to transition a module build to a new state with ModuleBuild.transition. This commit handles these two cases in a little different ways. For the former, existing code is refactored by moving the publish call outside ModuleBuild.create. For the latter, message is sent in a hook of SQLAlchemy ORM event after_commit rather than immediately inside the ModuleBuild.transition. Both of these changes ensure the message is sent after the changes are committed into database successfully. Then, the backend can have confidence that the database has the module build data when receive a message. Signed-off-by: Chenxiong Qi <cqi@redhat.com>
This commit is contained in:
27
tests/test_scheduler/test_db_session.py
Normal file
27
tests/test_scheduler/test_db_session.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from mock import patch
|
||||
|
||||
from module_build_service import conf
|
||||
from module_build_service.common import models
|
||||
from module_build_service.scheduler.db_session import db_session
|
||||
from tests import clean_database, make_module_in_db
|
||||
|
||||
|
||||
@patch('module_build_service.common.messaging.publish')
|
||||
def test_send_messages_after_several_state_transitions(mock_publish):
|
||||
"""
|
||||
Ensure all module build state change messages are sent after multiple
|
||||
ModuleBuild.transitions are committed at once
|
||||
"""
|
||||
clean_database()
|
||||
|
||||
build = make_module_in_db("testmodule:1:2:c3")
|
||||
|
||||
build.transition(db_session, conf, models.BUILD_STATES["wait"])
|
||||
build.transition(db_session, conf, models.BUILD_STATES["done"])
|
||||
|
||||
assert 0 == mock_publish.call_count
|
||||
db_session.commit()
|
||||
assert 2 == mock_publish.call_count
|
||||
@@ -138,10 +138,10 @@ class TestDecisionUpdateHandler:
|
||||
|
||||
# Assert this call below
|
||||
first_publish_call = call(
|
||||
service="mbs",
|
||||
topic="module.state.change",
|
||||
msg=module_build.json(db_session, show_tasks=False),
|
||||
conf=conf,
|
||||
"module.state.change",
|
||||
module_build.json(db_session, show_tasks=False),
|
||||
conf,
|
||||
"mbs",
|
||||
)
|
||||
|
||||
ClientSession.return_value.getBuild.return_value = {
|
||||
@@ -169,9 +169,9 @@ class TestDecisionUpdateHandler:
|
||||
publish.assert_has_calls([
|
||||
first_publish_call,
|
||||
call(
|
||||
service="mbs",
|
||||
topic="module.state.change",
|
||||
msg=module_build.json(db_session, show_tasks=False),
|
||||
conf=conf,
|
||||
"module.state.change",
|
||||
module_build.json(db_session, show_tasks=False),
|
||||
conf,
|
||||
"mbs"
|
||||
),
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user