Skip to content

Commit

Permalink
Try to launch android emulator and run in-app tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Sep 8, 2024
1 parent 15451ea commit 9e7ba05
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 129 deletions.
82 changes: 77 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,67 @@ jobs:
- name: Typecheck files
run: yarn typecheck

build-android:
# build-android:
# runs-on: ubuntu-latest
# env:
# TURBO_CACHE_DIR: .turbo/android
# steps:
# - name: Checkout
# uses: actions/checkout@v4

# - name: Turn off addons
# run: |
# node ./scripts/turnOffEverything.js

# - name: Setup
# uses: ./.github/actions/setup

# - name: Cache turborepo for Android
# uses: actions/cache@v3
# with:
# path: ${{ env.TURBO_CACHE_DIR }}
# key: ${{ runner.os }}-turborepo-android-${{ hashFiles('yarn.lock') }}
# restore-keys: |
# ${{ runner.os }}-turborepo-android-

# - name: Check turborepo cache for Android
# run: |
# TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:android').cache.status")

# if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
# echo "turbo_cache_hit=1" >> $GITHUB_ENV
# fi

# - name: Install JDK
# if: env.turbo_cache_hit != 1
# uses: actions/setup-java@v3
# with:
# distribution: 'zulu'
# java-version: '17'

# - name: Finalize Android SDK
# if: env.turbo_cache_hit != 1
# run: |
# /bin/bash -c "yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses > /dev/null"

# - name: Cache Gradle
# if: env.turbo_cache_hit != 1
# uses: actions/cache@v3
# with:
# path: |
# ~/.gradle/wrapper
# ~/.gradle/caches
# key: ${{ runner.os }}-gradle-${{ hashFiles('example/android/gradle/wrapper/gradle-wrapper.properties') }}
# restore-keys: |
# ${{ runner.os }}-gradle-

# - name: Build example for Android
# env:
# JAVA_OPTS: '-XX:MaxHeapSize=6g'
# run: |
# yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}"

test-android:
runs-on: ubuntu-latest
env:
TURBO_CACHE_DIR: .turbo/android
Expand Down Expand Up @@ -68,11 +128,23 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-
- name: Build example for Android
env:
JAVA_OPTS: '-XX:MaxHeapSize=6g'
- name: Enable KVM
run: |
yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}"
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: run tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 29
script: ./scripts test-android.sh

# - name: Build example for Android
# env:
# JAVA_OPTS: '-XX:MaxHeapSize=6g'
# run: |
# yarn turbo run android --cache-dir="${{ env.TURBO_CACHE_DIR }}"

build-ios:
runs-on: macos-latest
Expand Down
12 changes: 12 additions & 0 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ PODS:
- DoubleConversion (1.1.6)
- FBLazyVector (0.74.0)
- fmt (9.1.0)
- GCDWebServer (3.5.4):
- GCDWebServer/Core (= 3.5.4)
- GCDWebServer/Core (3.5.4)
- glog (0.3.5)
- hermes-engine (0.74.0):
- hermes-engine/Pre-built (= 0.74.0)
Expand Down Expand Up @@ -939,6 +942,9 @@ PODS:
- React-Mapbuffer (0.74.0):
- glog
- React-debug
- react-native-http-bridge-refurbished (1.2.9):
- GCDWebServer
- React
- react-native-restart (0.0.27):
- React-Core
- React-nativeconfig (0.74.0)
Expand Down Expand Up @@ -1206,6 +1212,7 @@ DEPENDENCIES:
- React-jsitracing (from `../node_modules/react-native/ReactCommon/hermes/executor/`)
- React-logger (from `../node_modules/react-native/ReactCommon/logger`)
- React-Mapbuffer (from `../node_modules/react-native/ReactCommon`)
- react-native-http-bridge-refurbished (from `../node_modules/react-native-http-bridge-refurbished`)
- react-native-restart (from `../node_modules/react-native-restart`)
- React-nativeconfig (from `../node_modules/react-native/ReactCommon`)
- React-NativeModulesApple (from `../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`)
Expand Down Expand Up @@ -1234,6 +1241,7 @@ DEPENDENCIES:

SPEC REPOS:
trunk:
- GCDWebServer
- SocketRocket

EXTERNAL SOURCES:
Expand Down Expand Up @@ -1300,6 +1308,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/logger"
React-Mapbuffer:
:path: "../node_modules/react-native/ReactCommon"
react-native-http-bridge-refurbished:
:path: "../node_modules/react-native-http-bridge-refurbished"
react-native-restart:
:path: "../node_modules/react-native-restart"
React-nativeconfig:
Expand Down Expand Up @@ -1356,6 +1366,7 @@ SPEC CHECKSUMS:
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
FBLazyVector: 026c8f4ae67b06e088ae01baa2271ef8a26c0e8c
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
GCDWebServer: 2c156a56c8226e2d5c0c3f208a3621ccffbe3ce4
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 6eae7edb2f563ee41d7c1f91f4f2e57c26d8a5c3
op-sqlite: 743e833721d4b4a0af7e7fefbff4375dc2d4e559
Expand Down Expand Up @@ -1383,6 +1394,7 @@ SPEC CHECKSUMS:
React-jsitracing: 36a2bbc272300313653d980de5ab700ec86c534a
React-logger: 03f2f7b955cfe24593a2b8c9705c23e142d1ad24
React-Mapbuffer: 5e05d78fe6505f4a054b86f415733d4ad02dd314
react-native-http-bridge-refurbished: e2e45508ec1573999ace69a0b880eee8f0e5bab2
react-native-restart: 7595693413fe3ca15893702f2c8306c62a708162
React-nativeconfig: 951ec32f632e81cbd7d40aebb3211313251c092e
React-NativeModulesApple: 0b3a42ca90069119ef79d8b2327d01441d71abd4
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"nativewind": "^2.0.11",
"react": "18.2.0",
"react-native": "0.74.0",
"react-native-http-bridge-refurbished": "1.2.9",
"react-native-restart": "^0.0.27",
"reflect-metadata": "^0.1.13",
"stream-browserify": "^3.0.0",
Expand Down
140 changes: 16 additions & 124 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import React, {useEffect, useState} from 'react';
import {
ActivityIndicator,
Button,
Clipboard,
SafeAreaView,
ScrollView,
Text,
View,
} from 'react-native';
import 'reflect-metadata';
import {createLargeDB, queryLargeDB} from './Database';
import {dbSetupTests, queriesTests, runTests, blobTests} from './tests/index';
import clsx from 'clsx';
import {styled} from 'nativewind';
import {useEffect, useState} from 'react';
import {SafeAreaView, ScrollView, Text, View} from 'react-native';
import {BridgeServer} from 'react-native-http-bridge-refurbished';
import 'reflect-metadata';
import {constantsTests} from './tests/constants.spec';
import {registerHooksTests} from './tests/hooks.spec';
import {moveAssetsDatabase, open} from '@op-engineering/op-sqlite';
import clsx from 'clsx';
import {blobTests, dbSetupTests, queriesTests, runTests} from './tests/index';
import {preparedStatementsTests} from './tests/preparedStatements.spec';
import {constantsTests} from './tests/constants.spec';
import {reactiveTests} from './tests/reactive.spec';

const StyledScrollView = styled(ScrollView, {
Expand All @@ -26,15 +17,7 @@ const StyledScrollView = styled(ScrollView, {
});

export default function App() {
const [isLoading, setIsLoading] = useState(false);
const [results, setResults] = useState<any>([]);
const [times, setTimes] = useState<number[]>([]);
const [accessingTimes, setAccessingTimes] = useState<number[]>([]);
const [prepareTimes, setPrepareTimes] = useState<number[]>([]);
const [prepareExecutionTimes, setPrepareExecutionTimes] = useState<number[]>(
[],
);
const [rawExecutionTimes, setRawExecutionTimes] = useState<number[]>([]);
useEffect(() => {
setResults([]);
runTests(
Expand All @@ -46,48 +29,19 @@ export default function App() {
constantsTests,
reactiveTests,
).then(setResults);
}, []);

const createLargeDb = async () => {
setIsLoading(true);
await createLargeDB();
setIsLoading(false);
};
const server = new BridgeServer('http_service', true);

const queryLargeDb = async () => {
try {
setIsLoading(true);
const times = await queryLargeDB();
setTimes(times.loadFromDb);
setAccessingTimes(times.access);
setPrepareTimes(times.prepare);
setPrepareExecutionTimes(times.preparedExecution);
setRawExecutionTimes(times.rawExecution);
} catch (e) {
console.error(e);
} finally {
setIsLoading(false);
}
};
server.get('/test_results', async (req, res) => {

Check failure on line 35 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'req' is declared but its value is never read.

Check failure on line 35 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'res' is declared but its value is never read.
return {results};
});

const copyDbPathToClipboad = async () => {
const db = await open({name: 'dbPath.sqlite', encryptionKey: 'test'});
db.execute(
'CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT)',
);
const path = db.getDbPath();
await db.close();
console.warn(path);
Clipboard.setString(path);
};
server.listen(3000);

const openAssetsDb = async () => {
const moved = await moveAssetsDatabase({filename: 'sample.sqlite'});
console.log('moved', moved);
const db = open({name: 'sample.sqlite'});
const users = await db.execute('SELECT * FROM User');
console.log('users', users.rows?._array);
};
return () => {
server.stop();
};
}, []);

let passingTests = 0;
let failingTests = 0;
Expand All @@ -105,68 +59,6 @@ export default function App() {
return (
<SafeAreaView className="flex-1 bg-neutral-900">
<StyledScrollView>
<View className="flex-row p-2 bg-neutral-800 items-center">
<Text className={'font-bold flex-1 text-white'}>Tools</Text>
</View>
<Button
title="Copy DB Path to clipboard"
onPress={copyDbPathToClipboad}
/>
<Button title="Open assets db" onPress={openAssetsDb} />
<Button title="Create 300k Record DB" onPress={createLargeDb} />
<Button title="Query 300k Records" onPress={queryLargeDb} />
{isLoading && <ActivityIndicator color={'white'} size="large" />}

{!!times.length && (
<Text className="text-lg text-white self-center">
Normal query{' '}
{(times.reduce((acc, t) => (acc += t), 0) / times.length).toFixed(
0,
)}{' '}
ms
</Text>
)}
{!!accessingTimes.length && (
<Text className="text-lg text-white self-center">
Read property{' '}
{(
accessingTimes.reduce((acc, t) => (acc += t), 0) /
accessingTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}
{!!prepareTimes.length && (
<Text className="text-lg text-white self-center">
Prepare statement{' '}
{(
prepareTimes.reduce((acc, t) => (acc += t), 0) /
prepareTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}
{!!prepareExecutionTimes.length && (
<Text className="text-lg text-white self-center">
Execute prepared query{' '}
{(
prepareExecutionTimes.reduce((acc, t) => (acc += t), 0) /
prepareExecutionTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}
{!!rawExecutionTimes.length && (
<Text className="text-lg text-white self-center">
Raw execution:{' '}
{(
rawExecutionTimes.reduce((acc, t) => (acc += t), 0) /
rawExecutionTimes.length
).toFixed(0)}{' '}
ms
</Text>
)}

<Text
className={clsx('font-bold flex-1 text-white p-2 mt-4', {
'bg-green-500': allTestsPassed,
Expand Down
58 changes: 58 additions & 0 deletions scripts/poll-in-app-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const http = require('http');

async function pollInAppServer() {
const startTime = Date.now();
const maxDuration = 5 * 60 * 1000; // 5 minutes
const pollInterval = 1000; // 1 second

while (Date.now() - startTime < maxDuration) {
try {
const response = await makeHttpRequest(
'http://localhost:3000/test_results'
);
if (response !== null) {
let parsed_response = JSON.parse(response);
const allTestsPassed = parsed_response.results.reduce((acc, r) => {
return acc && r.type !== 'incorrect';
}, true);

if (allTestsPassed) {
console.log('🟢🟢🟢🟢🟢 All tests passed!');
process.exit(0);
} else {
console.log('Some tests failed');
process.exit(1);
}
}
} catch (error) {
console.error('Error occurred during polling:', error);
}

await new Promise((resolve) => setTimeout(resolve, pollInterval));
}

console.error('Polling failed after 5 minutes');
process.exit(1);
}

function makeHttpRequest(url) {
return new Promise((resolve, reject) => {
http
.get(url, (res) => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
resolve(data);
});
})
.on('error', (error) => {
reject(error);
});
});
}

pollInAppServer();
Loading

0 comments on commit 9e7ba05

Please sign in to comment.