-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spec-tests): enforce spec-tests in CI (#400)
- Loading branch information
Showing
6 changed files
with
165 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Testing era_test_node against ETH spec | ||
on: | ||
workflow_call: | ||
|
||
jobs: | ||
spec: | ||
runs-on: ubuntu-latest | ||
name: spec | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: "recursive" | ||
|
||
- name: Download artifacts | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: era_test_node-ubuntu-latest.tar.gz | ||
|
||
- name: Extract era_test_node | ||
id: extract_node | ||
run: | | ||
echo "Extracting era_test_node binary" | ||
tar -xzf era_test_node-ubuntu-latest.tar.gz | ||
chmod +x era_test_node | ||
- name: Build ETH spec | ||
id: build_eth_spec | ||
working-directory: ./execution-apis | ||
run: npm install && npm run build | ||
|
||
- name: Launch tests | ||
id: launch | ||
working-directory: ./spec-tests | ||
run: cargo test | ||
env: | ||
ERA_TEST_NODE_BINARY_PATH: ../era_test_node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,72 @@ | ||
use anyhow::Context; | ||
use fs2::FileExt; | ||
use std::{ | ||
fs::File, | ||
net::{Ipv4Addr, SocketAddrV4}, | ||
}; | ||
|
||
use anyhow::Context; | ||
use fs2::FileExt; | ||
use tokio::net::TcpListener; | ||
|
||
/// Request an unused port from the OS. | ||
async fn pick_unused_port() -> anyhow::Result<u16> { | ||
// Port 0 means the OS gives us an unused port | ||
let addr = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0); | ||
let listener = TcpListener::bind(addr) | ||
.await | ||
.context("failed to bind to random port")?; | ||
let port = listener | ||
.local_addr() | ||
.context("failed to get local address for random port")? | ||
.port(); | ||
Ok(port) | ||
pub struct LockedPort { | ||
pub port: u16, | ||
lockfile: File, | ||
} | ||
|
||
/// Acquire an unused port and lock it for the duration until the era-test-node server has | ||
/// been started. | ||
pub async fn acquire_unused_port() -> anyhow::Result<(u16, File)> { | ||
loop { | ||
let port = pick_unused_port().await?; | ||
impl LockedPort { | ||
/// Checks if the requested port is free. | ||
/// Returns the unused port (same value as input, except for `0`). | ||
async fn check_port_is_unused(port: u16) -> anyhow::Result<u16> { | ||
let addr = SocketAddrV4::new(Ipv4Addr::LOCALHOST, port); | ||
let listener = TcpListener::bind(addr) | ||
.await | ||
.context("failed to bind to random port")?; | ||
let port = listener | ||
.local_addr() | ||
.context("failed to get local address for random port")? | ||
.port(); | ||
Ok(port) | ||
} | ||
|
||
/// Request an unused port from the OS. | ||
async fn pick_unused_port() -> anyhow::Result<u16> { | ||
// Port 0 means the OS gives us an unused port | ||
Self::check_port_is_unused(0).await | ||
} | ||
|
||
/// Acquire an unused port and lock it (meaning no other competing callers of this method can | ||
/// take this lock). Lock lasts until the returned `LockedPort` instance is dropped. | ||
pub async fn acquire_unused() -> anyhow::Result<Self> { | ||
loop { | ||
let port = Self::pick_unused_port().await?; | ||
let lockpath = std::env::temp_dir().join(format!("era-test-node-port{}.lock", port)); | ||
let lockfile = File::create(lockpath) | ||
.with_context(|| format!("failed to create lockfile for port={}", port))?; | ||
if lockfile.try_lock_exclusive().is_ok() { | ||
break Ok(Self { port, lockfile }); | ||
} | ||
} | ||
} | ||
|
||
/// Acquire the requested port and lock it (meaning no other competing callers of this method | ||
/// can take this lock). Lock lasts until the returned `LockedPort` instance is dropped. | ||
pub async fn acquire(port: u16) -> anyhow::Result<Self> { | ||
let port = Self::check_port_is_unused(port).await?; | ||
let lockpath = std::env::temp_dir().join(format!("era-test-node-port{}.lock", port)); | ||
let lockfile = File::create(lockpath) | ||
.with_context(|| format!("failed to create lockfile for port {}", port))?; | ||
if lockfile.try_lock_exclusive().is_ok() { | ||
break Ok((port, lockfile)); | ||
} | ||
.with_context(|| format!("failed to create lockfile for port={}", port))?; | ||
lockfile | ||
.try_lock_exclusive() | ||
.with_context(|| format!("failed to lock the lockfile for port={}", port))?; | ||
Ok(Self { port, lockfile }) | ||
} | ||
} | ||
|
||
/// Dropping `LockedPort` unlocks the port, caller needs to make sure the port is already bound to | ||
/// or is not needed anymore. | ||
impl Drop for LockedPort { | ||
fn drop(&mut self) { | ||
self.lockfile | ||
.unlock() | ||
.with_context(|| format!("failed to unlock lockfile for port={}", self.port)) | ||
.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters