Skip to content
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

Introduce Integration Tests #39

Merged
merged 26 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ on:
push:
tags:
- "v*.*.*"
workflow_call:

env:
CARGO_TERM_COLOR: always

jobs:
test_server:
uses: ./.github/workflows/test.yml
build:
name: Build ENState 🚀
runs-on: ubuntu-latest
needs: [test_server]
env:
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -20,7 +27,10 @@ jobs:
rustup set auto-self-update disable
rustup toolchain install stable --profile minimal

- uses: Swatinem/rust-cache@v2
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
with:
version: "v0.7.4"

- run: cargo build --release
working-directory: server
Expand Down
24 changes: 10 additions & 14 deletions .github/workflows/pr_check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Deploy
name: PR Check
on:
pull_request:
branches:
Expand All @@ -19,6 +19,9 @@ jobs:
target: x86_64-unknown-linux-gnu
- path: worker
target: wasm32-unknown-unknown
env:
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -29,20 +32,13 @@ jobs:
rustup toolchain install stable --profile minimal
rustup target add ${{ matrix.target }}

- name: Set up cargo cache
uses: actions/cache@v3
continue-on-error: false
- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
server/target/
worker/target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
version: "v0.7.4"

- run: cargo check --target ${{ matrix.target }} --release
working-directory: ${{ matrix.path }}

test:
uses: ./.github/workflows/test.yml
needs: [check]
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
on:
workflow_call:

jobs:
test:
name: Test ENState 🚀
runs-on: ubuntu-latest
env:
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
strategy:
matrix:
suite: [server, worker]
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- run: |
rustup set auto-self-update disable
rustup toolchain install stable --profile minimal

- name: Run sccache-cache
uses: mozilla-actions/sccache-action@v0.0.3
with:
version: "v0.7.4"

- uses: oven-sh/setup-bun@v1

- run: bun install
working-directory: test

- run: bun install --global wrangler
if: ${{ matrix.suite == 'worker' }}

- name: Test
run: bun test ${{ matrix.suite }}
working-directory: test
env:
RPC_URL: https://rpc.ankr.com/eth
OPENSEA_API_KEY: ${{ secrets.OPENSEA_API_KEY }}
2 changes: 1 addition & 1 deletion server/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use redis::aio::ConnectionManager;

pub async fn setup() -> Result<ConnectionManager> {
let redis = redis::Client::open(env::var("REDIS_URL").expect("REDIS_URL should've been set"))?;
let redis = redis::Client::open(env::var("REDIS_URL")?)?;

Ok(ConnectionManager::new(redis).await?)
}
18 changes: 14 additions & 4 deletions server/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use enstate_shared::cache::{CacheLayer, PassthroughCacheLayer};
use std::env;
use std::sync::Arc;

Expand All @@ -6,7 +7,7 @@ use enstate_shared::models::{
multicoin::cointype::{coins::CoinType, Coins},
records::Records,
};
use tracing::info;
use tracing::{info, warn};

use crate::provider::RoundRobin;
use crate::{cache, database};
Expand Down Expand Up @@ -43,9 +44,18 @@ impl AppState {

info!("Connecting to Redis...");

let redis = database::setup().await.expect("Redis connection failed");
let cache = database::setup().await.map_or_else(
|_| {
warn!("failed to connect to redis, using no cache");

info!("Connected to Redis");
Box::new(PassthroughCacheLayer {}) as Box<dyn CacheLayer>
},
|redis| {
info!("Connected to Redis");

Box::new(cache::Redis::new(redis)) as Box<dyn CacheLayer>
},
);

let provider = RoundRobin::new(rpc_urls);

Expand All @@ -54,7 +64,7 @@ impl AppState {

Self {
service: ProfileService {
cache: Box::new(cache::Redis::new(redis)),
cache,
rpc: Box::new(provider),
opensea_api_key,
profile_records: Arc::from(profile_records),
Expand Down
14 changes: 14 additions & 0 deletions shared/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ pub trait CacheLayer: Send + Sync {
async fn get(&self, key: &str) -> Result<String, CacheError>;
async fn set(&self, key: &str, value: &str, expires: u32) -> Result<(), CacheError>;
}

pub struct PassthroughCacheLayer {}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl CacheLayer for PassthroughCacheLayer {
async fn get(&self, _key: &str) -> Result<String, CacheError> {
Err(CacheError::Other("".to_string()))
}

async fn set(&self, _key: &str, _value: &str, _expires: u32) -> Result<(), CacheError> {
Ok(())
}
}
24 changes: 24 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2021
},
"extends": [
"plugin:v3xlabs/recommended"
],
"ignorePatterns": [
"!**/*"
],
"plugins": [
"v3xlabs"
],
"env": {
"node": true
},
"globals": {
"Bun": false
},
"rules": {
"sonarjs/no-duplicate-string": "off"
}
}
175 changes: 175 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
6 changes: 6 additions & 0 deletions test/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 4,
"useTabs": false,
"singleQuote": true,
"printWidth": 100
}
Loading