1

I was trying to test my anchor program with rust tests. Test written in Typescript are working fine. I am getting with cargo test:

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'testing_initialize' panicked at 'called `Result::unwrap()` on an `Err` value: IoError(Custom { kind: Other, error: "the request exceeded its deadline" })', programs/playground/tests/initialize.rs:66:10

Here is program Code:

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod playground {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        println!("Program Log: {:?}, {:?}, {:?}",ctx.accounts.global_state.key(),ctx.accounts.owner,ctx.accounts.global_state.key() );
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(
        mut,
    )]
    pub owner: Signer<'info>,
    #[account(
        init,
        payer = owner,
        seeds = ["seed".as_bytes()],
        bump,
        space = 200+8,
    )]
    pub global_state: Box<Account<'info, GlobalState>>,
    pub rent: Sysvar<'info, Rent>,
    pub system_program: Program<'info, System>,
}

#[account]
#[derive(Default)]
pub struct GlobalState {
    pub counter: u32,
}

Test file :

use anchor_lang::{system_program, solana_program::{self}, prelude::Pubkey};
use solana_program_test::{ProgramTest, processor, tokio};
use solana_sdk::{account::Account, instruction::Instruction, signature::Keypair, transaction::Transaction, signer::Signer};
use anchor_lang::InstructionData;
use anchor_lang::ToAccountMetas;

#[tokio::test]
async fn testing_initialize() {
    let mut program = ProgramTest::new(
        "anchor playground",
        playground::id(),
        // the entrypoint function is generated by the #[program] macro
        processor!(playground::entry),
    );

    let system_program = system_program::ID;
    let token_program_id = anchor_spl::token::ID;
    let rent = solana_program::sysvar::rent::ID;
    let associated_token_program_id = anchor_spl::associated_token::ID;
    println!(
        "{} / {} / {} / {}",
        system_program, token_program_id, rent, associated_token_program_id
    );
    
    let owner = Keypair::new();

    program.add_account(
        owner.pubkey(),
        Account {
            lamports: 1_000_000_000,
            ..Account::default()
        },
    );
    let global_state = Pubkey::find_program_address(&[b"seed"], &playground::id()).0;

    program.add_account(
        owner.pubkey(),
        Account {
            lamports: 1_000_000_000,
            ..Account::default()
        },
    );

    let initalize_ix =Instruction {
        program_id: playground::ID,
        data: playground::instruction::Initialize{}
        .data(),
        accounts: playground::accounts::Initialize {
            owner: owner.pubkey(),
            global_state,
            rent,
            system_program,
        }
        .to_account_metas(None),
    };
    let mut program_context = program.start_with_context().await;

    let mut initalize_tx = Transaction::new_with_payer(&[initalize_ix],Some(&owner.pubkey()));
    let recent_blockhash = program_context.last_blockhash.clone();

    initalize_tx.partial_sign(&[&owner], recent_blockhash);
    program_context
        .banks_client
        .process_transaction(initalize_tx)
        .await
        .unwrap();
}

also the cargo.toml file:

[package]
name = "playground"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "playground"

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.24.2"

[dev-dependencies]
solana-program-test = "1.9.29"
anchor-spl = "0.24.2"
solana-sdk = "1.9.29"

Full Logs with RUST_BACKTRACE

note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread 'testing_initialize' panicked at 'called `Result::unwrap()` on an `Err` value: IoError(Custom { kind: Other, error: "the request exceeded its deadline" })', programs/playground/tests/initialize.rs:66:10
stack backtrace:
   0: rust_begin_unwind
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/result.rs:1785:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/result.rs:1078:23
   4: initialize::testing_initialize::{{closure}}
             at ./tests/initialize.rs:62:5
   5: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/future/mod.rs:91:19
   6: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/future/future.rs:124:9
   7: tokio::runtime::basic_scheduler::Inner<P>::block_on::{{closure}}::{{closure}}
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/basic_scheduler.rs:240:62
   8: tokio::coop::with_budget::{{closure}}
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/coop.rs:106:9
   9: std::thread::local::LocalKey<T>::try_with
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/thread/local.rs:445:16
  10: std::thread::local::LocalKey<T>::with
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/thread/local.rs:421:9
  11: tokio::coop::with_budget
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/coop.rs:99:5
  12: tokio::coop::budget
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/coop.rs:76:5
  13: tokio::runtime::basic_scheduler::Inner<P>::block_on::{{closure}}
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/basic_scheduler.rs:240:39
  14: tokio::runtime::basic_scheduler::enter::{{closure}}
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/basic_scheduler.rs:349:29
  15: tokio::macros::scoped_tls::ScopedKey<T>::set
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/macros/scoped_tls.rs:61:9
  16: tokio::runtime::basic_scheduler::enter
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/basic_scheduler.rs:349:5
  17: tokio::runtime::basic_scheduler::Inner<P>::block_on
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/basic_scheduler.rs:230:9
  18: tokio::runtime::basic_scheduler::InnerGuard<P>::block_on
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/basic_scheduler.rs:501:9
  19: tokio::runtime::basic_scheduler::BasicScheduler<P>::block_on
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/basic_scheduler.rs:186:24
  20: tokio::runtime::Runtime::block_on
             at /Users/sushantchandla/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.14.1/src/runtime/mod.rs:461:46
  21: initialize::testing_initialize
             at ./tests/initialize.rs:62:5
  22: initialize::testing_initialize::{{closure}}
             at ./tests/initialize.rs:8:7
  23: core::ops::function::FnOnce::call_once
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/ops/function.rs:248:5
  24: core::ops::function::FnOnce::call_once
             at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


failures:
    testing_initialize

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 60.16s

Also the link to the Repo: https://github.com/SushantChandla/anchor-playground

1 Answer 1

3

Try running the test with cargo test-bpf and specify the tests. So command is:

cargo test-bpf --test initialize -- testing_iniitalize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.