forked from txpipe/oura
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tweak integration tests #10
Merged
paweljakubas
merged 3 commits into
paweljakubas/integration-tests
from
anviking/integration-test-tweaks
Nov 12, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,13 +6,19 @@ use std::time::Duration; | |
use anyhow::Result; | ||
use futures_util::SinkExt; | ||
use oura::sources::hydra::{HydraMessage, HydraMessagePayload}; | ||
use oura::sinks::Config::FileRotate; | ||
use serde_json::json; | ||
use tokio::net::TcpListener; | ||
use oura::sources::Config::Hydra; | ||
use oura::daemon::{run_daemon, ConfigRoot}; | ||
use gasket::daemon::Daemon; | ||
use goldenfile::Mint; | ||
use std::io::Write; | ||
use tempfile::NamedTempFile; | ||
use tokio::runtime::Runtime; | ||
use tokio::time; | ||
use tokio_tungstenite::accept_async; | ||
use port_selector::random_free_port; | ||
use tokio_tungstenite::tungstenite::protocol::Message; | ||
|
||
type TestResult = Result<(), Box<dyn std::error::Error>>; | ||
|
@@ -455,74 +461,92 @@ fn scenario_2() -> TestResult { | |
|
||
#[test] | ||
fn hydra_oura_stdout_scenario_1() -> TestResult { | ||
let golden_jsons = fs::read_to_string("tests/hydra/golden_1.txt")?; | ||
hydra_oura_stdout_test("tests/hydra/scenario_1.txt".to_string(), golden_jsons) | ||
hydra_oura_stdout_test("tests/hydra/scenario_1.txt".to_string(), "golden_1".to_string()) | ||
} | ||
|
||
#[test] | ||
fn hydra_oura_stdout_scenario_2() -> TestResult { | ||
let golden_jsons = fs::read_to_string("tests/hydra/golden_2.txt")?; | ||
hydra_oura_stdout_test("tests/hydra/scenario_2.txt".to_string(), golden_jsons) | ||
hydra_oura_stdout_test("tests/hydra/scenario_2.txt".to_string(), "golden_2".to_string()) | ||
} | ||
|
||
// Run: | ||
// cargo test hydra_oura -- --nocapture | ||
// in order to see println | ||
fn hydra_oura_stdout_test(file: String, expected: String) -> TestResult { | ||
fn hydra_oura_stdout_test(scenario_file: String, golden_name: String) -> TestResult { | ||
|
||
let mut mint = Mint::new("tests/hydra"); | ||
let mut golden = mint.new_goldenfile(golden_name.clone()).unwrap(); | ||
|
||
let rt = Runtime::new().unwrap(); | ||
let _ = rt.block_on(async move { | ||
let addr = "127.0.0.1:4001".to_string(); | ||
let port: u16 = random_free_port().unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
let addr= format!("127.0.0.1:{}", port); | ||
let server = TcpListener::bind(&addr).await?; | ||
println!("WebSocket server started on ws://{}", addr); | ||
let output_file = NamedTempFile::new()?; | ||
let config = test_config(&output_file, &addr); | ||
|
||
let _ = tokio::spawn(async move { oura_pipeline().await }); | ||
println!("WebSocket server starting on ws://{}", addr); | ||
|
||
mock_hydra_node(server, file).await; | ||
|
||
let emitted_jsons = fs::read_to_string("tests/hydra/logs.txt")?; | ||
assert_eq!(emitted_jsons, expected); | ||
let _ = tokio::spawn(async move { run_oura(config) }); | ||
let _ = mock_hydra_node(server, scenario_file).await; | ||
|
||
// After the connection is established, give oura time to process | ||
// the chain data and write the output to disk. | ||
time::sleep(Duration::from_secs(3)).await; | ||
let emitted_jsons= fs::read_to_string(output_file)?; | ||
golden.write_all(emitted_jsons.as_bytes()).unwrap(); | ||
println!("test done, will exit"); | ||
Ok::<(), std::io::Error>(()) | ||
}); | ||
Ok(()) | ||
} | ||
|
||
/// Will await the first connection, and then return while handling it in the | ||
/// background. | ||
async fn mock_hydra_node(server: TcpListener, file: String) { | ||
let (tx, _rx) = mpsc::channel(); | ||
while let Ok((stream, _)) = server.accept().await { | ||
tokio::spawn(handle_connection(stream, file, tx)); | ||
time::sleep(Duration::from_secs(3)).await; | ||
break; | ||
async fn handle_connection( | ||
stream: tokio::net::TcpStream, | ||
file: String, | ||
tx: mpsc::Sender<usize>, | ||
) -> Result<()> { | ||
let mut ws_stream = accept_async(stream).await?; | ||
println!("WebSocket server oura connection established"); | ||
|
||
let to_send = fs::read_to_string(file)?; | ||
|
||
let mut lines = 0; | ||
for line in to_send.lines() { | ||
ws_stream.send(Message::Text(line.to_string())).await?; | ||
lines += 1; | ||
} | ||
tx.send(lines).unwrap(); | ||
Ok(()) | ||
} | ||
|
||
let (tx, _rx) = mpsc::channel(); | ||
let (stream, _) = server.accept().await.unwrap(); | ||
let _ = tokio::spawn(handle_connection(stream, file, tx)); | ||
} | ||
|
||
async fn handle_connection( | ||
stream: tokio::net::TcpStream, | ||
file: String, | ||
tx: mpsc::Sender<usize>, | ||
) -> Result<()> { | ||
let mut ws_stream = accept_async(stream).await?; | ||
println!("WebSocket server oura connection established"); | ||
|
||
let to_send = fs::read_to_string(file)?; | ||
fn test_config(tmp_output_file: &NamedTempFile, socket_path: &String) -> ConfigRoot { | ||
let mut config = ConfigRoot::new(&Some(PathBuf::from("tests/daemon.toml"))).unwrap(); | ||
|
||
let mut lines = 0; | ||
for line in to_send.lines() { | ||
ws_stream.send(Message::Text(line.to_string())).await?; | ||
lines += 1; | ||
if let FileRotate(ref mut file_rotate) = config.sink { | ||
file_rotate.output_path = Some(tmp_output_file.path().to_string_lossy().to_string()); | ||
} else { | ||
panic!("assumed config template to use file_rotate sink"); | ||
} | ||
tx.send(lines).unwrap(); | ||
|
||
Ok(()) | ||
} | ||
if let Hydra(ref mut hydra_config) = config.source { | ||
hydra_config.hydra_socket_url = "ws://".to_string() + socket_path; | ||
} else { | ||
panic!("assumed config template to use hydra source"); | ||
} | ||
|
||
async fn oura_pipeline() -> Result<()> { | ||
tokio::spawn(invoke_pipeline()); | ||
time::sleep(Duration::from_secs(1)).await; | ||
Ok(()) | ||
config | ||
} | ||
|
||
async fn invoke_pipeline() -> Result<Daemon> { | ||
let config = ConfigRoot::new(&Some(PathBuf::from("tests/daemon.toml")))?; | ||
fn run_oura(config: ConfigRoot) -> Result<Daemon> { | ||
run_daemon(config).map_err(|e| anyhow::anyhow!(e)) | ||
} |
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍