From c7aab54db2a9bb30467ec1e63a5d0b524baa95b9 Mon Sep 17 00:00:00 2001
From: ordian <noreply@reusable.software>
Date: Tue, 7 Jan 2025 11:17:30 +0100
Subject: [PATCH] fix cumulus runtime_upgrade_events test

---
 cumulus/pallets/parachain-system/src/mock.rs  |  1 +
 cumulus/pallets/parachain-system/src/tests.rs | 23 +++++++++++++++----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/cumulus/pallets/parachain-system/src/mock.rs b/cumulus/pallets/parachain-system/src/mock.rs
index 5b59be0482e7..7503dab49fe1 100644
--- a/cumulus/pallets/parachain-system/src/mock.rs
+++ b/cumulus/pallets/parachain-system/src/mock.rs
@@ -449,6 +449,7 @@ impl BlockTests {
 			}
 
 			// clean up
+			System::maybe_apply_pending_code_upgrade();
 			let header = System::finalize();
 			let head_data = relay_chain::HeadData(header.encode());
 			parent_head_data = head_data.clone();
diff --git a/cumulus/pallets/parachain-system/src/tests.rs b/cumulus/pallets/parachain-system/src/tests.rs
index 2b65dd6a9216..f57133c80d6f 100755
--- a/cumulus/pallets/parachain-system/src/tests.rs
+++ b/cumulus/pallets/parachain-system/src/tests.rs
@@ -443,7 +443,7 @@ fn hrmp_outbound_respects_used_bandwidth() {
 fn runtime_upgrade_events() {
 	BlockTests::new()
 		.with_relay_sproof_builder(|_, block_number, builder| {
-			if block_number > 123 {
+			if block_number == 1234 {
 				builder.upgrade_go_ahead = Some(relay_chain::UpgradeGoAhead::GoAhead);
 			}
 		})
@@ -466,21 +466,34 @@ fn runtime_upgrade_events() {
 			|| {
 				let events = System::events();
 
-				assert_eq!(events[0].event, RuntimeEvent::System(frame_system::Event::CodeUpdated));
-
 				assert_eq!(
-					events[1].event,
+					events[0].event,
 					RuntimeEvent::ParachainSystem(crate::Event::ValidationFunctionApplied {
 						relay_chain_block_num: 1234
 					})
 				);
 
+				assert!(System::digest()
+					.logs()
+					.iter()
+					.all(|d| *d != sp_runtime::generic::DigestItem::RuntimeEnvironmentUpdated));
+			},
+		)
+		.add_with_post_test(
+			1235,
+			|| {},
+			|| {
+				let events = System::events();
+
+				assert_eq!(events[0].event, RuntimeEvent::System(frame_system::Event::CodeUpdated));
+
 				assert!(System::digest()
 					.logs()
 					.iter()
 					.any(|d| *d == sp_runtime::generic::DigestItem::RuntimeEnvironmentUpdated));
 			},
-		);
+		)
+	;
 }
 
 #[test]