Skip to content

Commit

Permalink
Merge branch 'main' into feat/solidity-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agostbiro committed Nov 6, 2024
2 parents 20e290f + e0c927d commit 8e88733
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
26 changes: 17 additions & 9 deletions crates/edr_napi/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{io, ops::Deref, sync::Arc};
use std::{ops::Deref, sync::Arc};

#[cfg(feature = "tracing")]
use napi::Status;
use napi_derive::napi;
use tracing_subscriber::{prelude::*, EnvFilter, Registry};
Expand All @@ -23,8 +24,7 @@ impl EdrContext {
#[doc = "Creates a new [`EdrContext`] instance. Should only be called once!"]
#[napi(constructor)]
pub fn new() -> napi::Result<Self> {
let context =
Context::new().map_err(|e| napi::Error::new(Status::GenericFailure, e.to_string()))?;
let context = Context::new()?;

Ok(Self {
inner: Arc::new(context),
Expand All @@ -34,14 +34,13 @@ impl EdrContext {

#[derive(Debug)]
pub struct Context {
_subscriber_guard: tracing::subscriber::DefaultGuard,
#[cfg(feature = "tracing")]
_tracing_write_guard: tracing_flame::FlushGuard<std::io::BufWriter<std::fs::File>>,
}

impl Context {
/// Creates a new [`Context`] instance. Should only be called once!
pub fn new() -> io::Result<Self> {
pub fn new() -> napi::Result<Self> {
let fmt_layer = tracing_subscriber::fmt::layer()
.with_file(true)
.with_line_number(true)
Expand All @@ -54,8 +53,13 @@ impl Context {

#[cfg(feature = "tracing")]
let (flame_layer, guard) = {
let (flame_layer, guard) =
tracing_flame::FlameLayer::with_file("tracing.folded").unwrap();
let (flame_layer, guard) = tracing_flame::FlameLayer::with_file("tracing.folded")
.map_err(|err| {
napi::Error::new(
Status::GenericFailure,
format!("Failed to create tracing.folded file with error: {err:?}"),
)
})?;

let flame_layer = flame_layer.with_empty_samples(false);
(flame_layer, guard)
Expand All @@ -64,10 +68,14 @@ impl Context {
#[cfg(feature = "tracing")]
let subscriber = subscriber.with(flame_layer);

let subscriber_guard = tracing::subscriber::set_default(subscriber);
if let Err(error) = tracing::subscriber::set_global_default(subscriber) {
println!(
"Failed to set global tracing subscriber with error: {error}\n\
Please only initialize EdrContext once per process to avoid this error."
);
}

Ok(Self {
_subscriber_guard: subscriber_guard,
#[cfg(feature = "tracing")]
_tracing_write_guard: guard,
})
Expand Down
8 changes: 8 additions & 0 deletions crates/edr_napi/test/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EdrContext } from "../index";

describe("EdrContext", () => {
it("EdrContext doesn't throw if initialized twice", () => {
new EdrContext();
new EdrContext();
});
});
16 changes: 15 additions & 1 deletion crates/edr_napi/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { TracingMessage, TracingMessageResult, TracingStep } from "..";
import {
EdrContext,
TracingMessage,
TracingMessageResult,
TracingStep,
} from "..";

function getEnv(key: string): string | undefined {
const variable = process.env[key];
Expand All @@ -17,6 +22,15 @@ export function isCI(): boolean {
return getEnv("CI") === "true";
}

let context: EdrContext | undefined;

export function getContext(): EdrContext {
if (context === undefined) {
context = new EdrContext();
}
return context;
}

/**
* Given a trace, return only its steps.
*/
Expand Down
5 changes: 2 additions & 3 deletions crates/edr_napi/test/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { JsonStreamStringify } from "json-stream-stringify";

import {
ContractAndFunctionName,
EdrContext,
MineOrdering,
Provider,
SpecId,
SubscriptionEvent,
} from "..";
import { ALCHEMY_URL, isCI } from "./helpers";
import { ALCHEMY_URL, getContext, isCI } from "./helpers";

describe("Provider", () => {
const context = new EdrContext();
const context = getContext();
const providerConfig = {
allowBlocksWithSameTimestamp: false,
allowUnlimitedContractSize: true,
Expand Down
10 changes: 7 additions & 3 deletions crates/edr_napi/test/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import chaiAsPromised from "chai-as-promised";

import {
ContractAndFunctionName,
EdrContext,
MineOrdering,
Provider,
SpecId,
SubscriptionEvent,
} from "..";
import { collectMessages, collectSteps, ALCHEMY_URL } from "./helpers";
import {
collectMessages,
collectSteps,
ALCHEMY_URL,
getContext,
} from "./helpers";

chai.use(chaiAsPromised);

describe("Provider", () => {
const context = new EdrContext();
const context = getContext();
const providerConfig = {
allowBlocksWithSameTimestamp: false,
allowUnlimitedContractSize: true,
Expand Down

0 comments on commit 8e88733

Please sign in to comment.