diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 85d545186..5d75f8432 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -11,65 +11,63 @@ on: workflows: ["BuildAndReleaseMaster"] types: - completed + jobs: invoke_jasmine_tests: name: Invoke Jasmine performance tests runs-on: self-hosted-runner-ubuntu steps: - - name: 'Cleanup build folder' + - name: Cleanup build folder run: | ls -la ./ - rm -rf ./* || true - rm -rf ./.??* || true + rm -rf ./* ./.??* || true ls -la ./ + - name: Checkout uses: actions/checkout@v3 - - name: Install pre-requisite dependencies + + - name: Install dependencies run: | - sudo apt-get update && sudo apt-get install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libnss3 lsb-release xdg-utils wget ca-certificates - sudo apt-get install -y libudev-dev cargo npm imagemagick libmagickwand-dev cmake jq - - name: install node + sudo apt-get update + sudo apt-get install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 \ + libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 \ + libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 \ + libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 \ + libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libnss3 lsb-release xdg-utils wget \ + libudev-dev cargo npm imagemagick libmagickwand-dev cmake jq + + - name: Install Node.js uses: actions/setup-node@v4 with: node-version: 22.4.0 - - name: Install ruby and other related tools + + - name: Install Ruby and related tools run: | whoami npm install -g corepack corepack enable curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list - apt-get update - sudo apt-get install -y yarn - yarn --version - apt update - apt-get install -y software-properties-common + sudo apt-get update && sudo apt-get install -y yarn software-properties-common apt-add-repository -y ppa:rael-gc/rvm - apt-get update - apt-get install -y rvm + sudo apt-get update && sudo apt-get install -y rvm echo 'source "/etc/profile.d/rvm.sh"' >> ~/.bashrc source /etc/profile.d/rvm.sh - which rvm rvm install ruby-3.1.2 ruby --version sudo chown -R $(whoami) /usr/local rvm use 3.1.2 --default - rvm --version - rvm info - which rvm - ruby --version - - name: cargo install nj-cli + + - name: Install cargo tools run: | - cargo install nj-cli --locked - cargo install wasm-pack --locked + cargo install nj-cli wasm-pack --locked + - name: Prepare environment run: | - npm i -g tslib + npm install -g tslib export PATH="/usr/share/rvm:$PATH" - which ruby - ruby --version - pwd gem install dotenv json octokit tmpdir fileutils + - name: Get PR details if: github.event_name == 'workflow_dispatch' uses: actions/github-script@v7 @@ -83,43 +81,26 @@ jobs: repo: 'chipmunk', pull_number: ${{ github.event.inputs.pr_id }} }); - const PR_OWNER = pr.data.user.login; - const PR_REPO = pr.data.base.repo.name; - console.log(`owner is ${PR_OWNER}`); - console.log(`repo is ${PR_REPO}`); - // Setting environment variables - fs.appendFileSync(process.env.GITHUB_ENV, `PR_OWNER=${PR_OWNER}\n`); - fs.appendFileSync(process.env.GITHUB_ENV, `PR_REPO=${PR_REPO}\n`); - - name: Run Jasmine performance tests for the latest release - if: github.event_name != 'workflow_dispatch' - run: | - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y - export PATH="/root/.cargo/bin:$PATH" - . "/root/.cargo/env" - source ~/.bashrc - ruby scripts/tools/run_benchmarks.rb 1 - env: - REPO_OWNER: 'esrlabs' - REPO_NAME: 'chipmunk' - - name: Run Jasmine performance tests for the latest pull request - if: github.event_name == 'workflow_dispatch' + fs.appendFileSync(process.env.GITHUB_ENV, `REPO_OWNER=${pr.data.user.login}\n`); + fs.appendFileSync(process.env.GITHUB_ENV, `REPO_NAME=${pr.data.base.repo.name}\n`); + + - name: Run Jasmine performance tests run: | curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y export PATH="/root/.cargo/bin:$PATH" - . "/root/.cargo/env" source ~/.bashrc - echo "PR head repo: ${{ env.PR_OWNER }}" - echo "PR head owner: ${{ env.PR_REPO }}" - pwd - ls -la - ruby scripts/tools/run_benchmarks.rb PR~${{ github.event.inputs.pr_id }} - env: - REPO_NAME: ${{ env.PR_REPO }} - REPO_OWNER: ${{ env.PR_OWNER }} + if [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then + ruby scripts/tools/run_benchmarks.rb PR~${{ github.event.inputs.pr_id }} + else + export REPO_OWNER='esrlabs' + export REPO_NAME='chipmunk' + ruby scripts/tools/run_benchmarks.rb 1 + fi + - name: List files in the results folder run: | - echo "Event is: ${{ github.event_name }}" ls -la /chipmunk/chipmunk_performance_results + push_data_to_chipmunk_docs: name: Move benchmark data to chipmunk-docs repository needs: invoke_jasmine_tests @@ -130,28 +111,21 @@ jobs: with: repository: esrlabs/chipmunk-docs path: './chipmunk-docs' - token: ${{secrets.DOCS_PUSH_TOKEN}} - - name: Push tag for release - working-directory: ./chipmunk-docs - if: github.event_name != 'workflow_dispatch' - run: | - ls -la - cp /chipmunk/chipmunk_performance_results/data.json ./jekyll/benchmarks/data/data.json - git config user.name "esrlabs" - git config user.email "esrlabs@gmail.com" - git remote set-url origin "https://esrlabs:${{secrets.DOCS_PUSH_TOKEN}}@github.com/esrlabs/chipmunk-docs" - git add ./jekyll/benchmarks/data/data.json - git commit -m "Updating data.json for latest tag" - git push origin master - - name: Push PR data to chipmunk-docs + token: ${{ secrets.DOCS_PUSH_TOKEN }} + + - name: Push benchmark data working-directory: ./chipmunk-docs - if: github.event_name == 'workflow_dispatch' run: | - ls -la - cp /chipmunk/chipmunk_performance_results/Benchmark_PR_${{ github.event.inputs.pr_id }}.json ./jekyll/benchmarks/data/pull_request/Benchmark_PR_${{ github.event.inputs.pr_id }}.json git config user.name "esrlabs" git config user.email "esrlabs@gmail.com" git remote set-url origin "https://esrlabs:${{secrets.DOCS_PUSH_TOKEN}}@github.com/esrlabs/chipmunk-docs" - git add ./jekyll/benchmarks/data/pull_request/Benchmark_PR_${{ github.event.inputs.pr_id }}.json - git commit -m "Adding PR benchmark results for chipmunk PR # ${{ github.event.inputs.pr_id }}" + if [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then + cp /chipmunk/chipmunk_performance_results/Benchmark_PR_${{ github.event.inputs.pr_id }}.json ./jekyll/benchmarks/data/pull_request/ + git add ./jekyll/benchmarks/data/pull_request/Benchmark_PR_${{ github.event.inputs.pr_id }}.json + git commit -m "Adding PR benchmark results for chipmunk PR #${{ github.event.inputs.pr_id }}" + else + cp /chipmunk/chipmunk_performance_results/data.json ./jekyll/benchmarks/data/data.json + git add ./jekyll/benchmarks/data/data.json + git commit -m "Updating data.json for latest tag" + fi git push origin master \ No newline at end of file diff --git a/application/apps/rustcore/ts-bindings/spec/benchmarks.json b/application/apps/rustcore/ts-bindings/spec/benchmarks.json index ac3ee3089..bb7478f43 100644 --- a/application/apps/rustcore/ts-bindings/spec/benchmarks.json +++ b/application/apps/rustcore/ts-bindings/spec/benchmarks.json @@ -1,58 +1,51 @@ -/* NOTE: in this JSON can be used comments placed between stars */ { "log_level": 1, "tests": { "benchmark": { - "performance": { - /* In false will prevent running performance tests */ - "run": true, - "tests": { - "test1": { - "alias": "Observe - grab content (text)", - "file": "test_files/temp_readings3.txt", - "expectation_ms": 10000 - }, - "test2": { - "alias": "Observe - grab content (dlt)", - "file": "test_files/FzgProg_SP21.dlt", - "expectation_ms": 60000 - }, - "test3": { - "alias": "Observe - grab content (pcapng)", - "file": "test_files/someip.pcapng", - "expectation_ms": 1000 - }, - "test4": { - "alias": "Stream - startup measurement", - "file": "", - "expectation_ms": 1000 - }, - "test5": { - "alias": "Stream - shutdown measurement", - "file": "", - "expectation_ms": 10000 - }, - "test6": { - "alias": "Stream - Open 50 sessions", - "file": "", - "expectation_ms": 10000 - }, - "test7": { - "alias": "Indexes - Switch to breadcrumb mode", - "file": "test_files/indexing_access_huge.log", - "expectation_ms": 15000 - }, - "test8": { - "alias": "Assign & single search", - "file": "test_files/indexing_access_huge.log", - "expectation_ms": 1000 - }, - "test9": { - "alias": "Assign & multiple search", - "file": "test_files/indexing_access_huge.log", - "expectation_ms": 10000 - } - } + "test1": { + "alias": "Observe - grab content (text)", + "file": "test_files/temp_readings3.txt", + "expectation_ms": 10000 + }, + "test2": { + "alias": "Observe - grab content (dlt)", + "file": "test_files/FzgProg_SP21.dlt", + "expectation_ms": 60000 + }, + "test3": { + "alias": "Observe - grab content (pcapng)", + "file": "test_files/someip.pcapng", + "expectation_ms": 1000 + }, + "test4": { + "alias": "Stream - startup measurement", + "file": "", + "expectation_ms": 1000 + }, + "test5": { + "alias": "Stream - shutdown measurement", + "file": "", + "expectation_ms": 10000 + }, + "test6": { + "alias": "Stream - Open 50 sessions", + "file": "", + "expectation_ms": 10000 + }, + "test7": { + "alias": "Indexes - Switch to breadcrumb mode", + "file": "test_files/indexing_access_huge.log", + "expectation_ms": 15000 + }, + "test8": { + "alias": "Assign & single search", + "file": "test_files/indexing_access_huge.log", + "expectation_ms": 1000 + }, + "test9": { + "alias": "Assign & multiple search", + "file": "test_files/indexing_access_huge.log", + "expectation_ms": 10000 } } } diff --git a/application/apps/rustcore/ts-bindings/spec/common.ts b/application/apps/rustcore/ts-bindings/spec/common.ts index d59fcee6c..a537770fa 100644 --- a/application/apps/rustcore/ts-bindings/spec/common.ts +++ b/application/apps/rustcore/ts-bindings/spec/common.ts @@ -1,15 +1,9 @@ // tslint:disable -// We need to provide path to TypeScript types definitions -/// -/// - import { Jobs, Tracker, Session } from '../src/index'; import { Logger, getLogger } from './logger'; import { error, numToLogLevel } from 'platform/log/utils'; import { state } from 'platform/log'; -import { IRegularTests } from './config'; -import { IPerformanceTest } from './config_benchmarks'; import * as tmp from 'tmp'; import * as fs from 'fs'; @@ -23,60 +17,6 @@ const MS_PER_SEC = 1000; // Get rid of default Jasmine timeout jasmine.DEFAULT_TIMEOUT_INTERVAL = 900000; -export type ScopeInjector = (s: T) => T; - -export function runner( - config: IRegularTests | IPerformanceTest, - id: string | number, - test: ( - logger: Logger, - done: () => void, - add: ScopeInjector, - ) => Promise, -): Promise { - const scope: Array = []; - const injector: ScopeInjector = (obj: Session | Tracker | Jobs) => { - scope.push(obj); - return obj; - }; - - let name: string; - let shouldExecute = true; - - if ('list' in config) { - // Handling IRegularTests - name = config.list[id]; - shouldExecute = config.execute_only.length === 0 || config.execute_only.includes(typeof id === 'number' ? id : parseInt(id, 10)); - } else if ('alias' in config) { - // Handling IPerformanceTest - name = config.alias; - shouldExecute = !config.ignore; - } else { - // Log the type of config received - console.error('Invalid configuration passed to runner. Config:', config); - return Promise.reject(new Error('Invalid configuration passed to runner')); - } - - const logger = getLogger(name); - - if (!shouldExecute) { - console.log(`\nIgnored: ${name}`); - return Promise.resolve(); - } else { - console.log(`\nStarted: ${name}`); - } - - return new Promise((done) => { - try { - test(logger, done, injector).catch((err: Error) => { - finish(scope, done, err); - }); - } catch (err) { - finish(scope, done, new Error(error(err))); - } - }); -} - export function readConfigFile(filenameEnvVar: string, defaultPaths: string[]): T | Error { const defaults = (() => { for (const target of defaultPaths) { @@ -299,7 +239,6 @@ export function performanceReport( output(`Missing necessary environment variables for file path.`); } - return actual <= expectation; } diff --git a/application/apps/rustcore/ts-bindings/spec/config_benchmarks.ts b/application/apps/rustcore/ts-bindings/spec/config_benchmarks.ts index ed0a53c38..86299a93f 100644 --- a/application/apps/rustcore/ts-bindings/spec/config_benchmarks.ts +++ b/application/apps/rustcore/ts-bindings/spec/config_benchmarks.ts @@ -13,11 +13,7 @@ export interface IPerformanceTest { export interface IConfiguration { log_level: number; tests: { - benchmark: { - performance: { - tests: { [key: string]: IPerformanceTest }; - }; - }; + benchmark: { [key: string]: IPerformanceTest }; }; } diff --git a/application/apps/rustcore/ts-bindings/spec/runners.ts b/application/apps/rustcore/ts-bindings/spec/runners.ts new file mode 100644 index 000000000..161e9b0a6 --- /dev/null +++ b/application/apps/rustcore/ts-bindings/spec/runners.ts @@ -0,0 +1,143 @@ +import { Jobs, Tracker, Session, SessionStream, ISessionEvents, SessionSearch } from '../src/index'; +import { Logger, getLogger } from './logger'; +import { error } from 'platform/log/utils'; +import { IRegularTests } from './config'; +import { IPerformanceTest } from './config_benchmarks'; +import { finish } from './common'; + +export interface SessionComponents { + session: Session; + stream: SessionStream; + events: ISessionEvents; + search: SessionSearch; +} + +export async function initializeSession(testName: string): Promise { + const session = await Session.create(); + session.debug(true, testName); + + let stream, events, search; + + stream = session.getStream(); + if (stream instanceof Error) throw stream; + + events = session.getEvents(); + if (events instanceof Error) throw events; + + search = session.getSearch(); + if (search instanceof Error) throw search; + + return { session, stream, events, search }; +} + +export type ScopeInjector = (s: T) => T; + +function validate( + config: IRegularTests | IPerformanceTest, + id: string | number, +): string | Promise { + let name: string; + let shouldExecute = true; + if ('list' in config) { + // Handling IRegularTests + name = config.list[id]; + shouldExecute = + config.execute_only.length === 0 || + config.execute_only.includes(typeof id === 'number' ? id : parseInt(id, 10)); + } else if ('alias' in config) { + // Handling IPerformanceTest + name = config.alias; + shouldExecute = !config.ignore; + } else { + // Log the type of config received + console.error('Invalid configuration passed to runner. Config:', config); + return Promise.reject(new Error('Invalid configuration passed to runner')); + } + if (!shouldExecute) { + console.log(`\nIgnored: ${name}`); + return Promise.resolve(void 0); + } else { + console.log(`\nStarted: ${name}`); + } + return name; +} + +export async function withSession( + config: IRegularTests | IPerformanceTest, + id: string | number, + test: (logger: Logger, done: () => void, components: SessionComponents) => Promise, +): Promise { + const name = validate(config, id); + if (typeof name !== 'string') { + return name; + } + const logger = getLogger(name); + return new Promise((done) => { + initializeSession(name) + .then((components) => { + const sessionRef = components.session; + try { + test(logger, done, components).catch((err: Error) => { + finish(sessionRef, done, err); + }); + } catch (err) { + finish(sessionRef, done, new Error(error(err))); + } + }) + .catch((err: Error) => { + logger.error(`Fail to init session due: ${err.message}`); + finish(undefined, done, new Error(error(err))); + }); + }); +} + +export async function noSession( + config: IRegularTests | IPerformanceTest, + id: string | number, + test: (logger: Logger, done: () => void) => Promise, +): Promise { + const name = validate(config, id); + if (typeof name !== 'string') { + return name; + } + const logger = getLogger(name); + return new Promise((done) => { + try { + test(logger, done).catch((err: Error) => { + finish(undefined, done, err); + }); + } catch (err) { + finish(undefined, done, new Error(error(err))); + } + }); +} + +export function unbound( + config: IRegularTests | IPerformanceTest, + id: string | number, + test: ( + logger: Logger, + done: () => void, + add: ScopeInjector, + ) => Promise, +): Promise { + const scope: Array = []; + const injector: ScopeInjector = (obj: Session | Tracker | Jobs) => { + scope.push(obj); + return obj; + }; + const name = validate(config, id); + if (typeof name !== 'string') { + return name; + } + const logger = getLogger(name); + return new Promise((done) => { + try { + test(logger, done, injector).catch((err: Error) => { + finish(scope, done, err); + }); + } catch (err) { + finish(scope, done, new Error(error(err))); + } + }); +} \ No newline at end of file diff --git a/application/apps/rustcore/ts-bindings/spec/session.benchmark.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.benchmark.spec.ts index 49f0e8214..e1880d5b4 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.benchmark.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.benchmark.spec.ts @@ -1,16 +1,14 @@ // tslint:disable -// We need to provide path to TypeScript types definitions -/// -/// import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IAttachmentsUpdatedUpdated } from '../src/api/session.provider'; import { IAttachment } from 'platform/types/content'; -import { createSampleFile, finish, performanceReport, setMeasurement, runner } from './common'; +import { createSampleFile, finish, performanceReport, setMeasurement } from './common'; import { readBenchmarkConfigurationFile } from './config_benchmarks'; import { IndexingMode } from 'platform/types/content'; +import * as runners from './runners'; import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; @@ -18,8 +16,8 @@ import * as path from 'path'; const config = readBenchmarkConfigurationFile().get().tests.benchmark; describe('Benchmark Tests', function () { - Object.keys(config.performance.tests).forEach((alias: string) => { - const test = config.performance.tests[alias]; + Object.keys(config).forEach((testId: string, index: number) => { + const test = config[testId]; const testName = `${test.alias}`; if (test.ignore) { console.log(`Test "${testName}" has been ignored`); @@ -27,94 +25,78 @@ describe('Benchmark Tests', function () { } it(testName, function () { - return runner( - { - open_as: '', - ignore: false, - alias: testName, - expectation_ms: 10000, - file: '', - }, - 1, - async (logger, done, collector) => { + return runners.withSession( + { + open_as: '', + ignore: false, + alias: testName, + expectation_ms: 10000, + file: '', + }, + 1, + async (logger, done, {session, stream, events, search}) => { const measurement = setMeasurement(); - try { - const session = await Session.create(); - session.debug(true, testName); - - const stream = session.getStream(); - if (stream instanceof Error) throw stream; - - const events = session.getEvents(); - if (events instanceof Error) throw events; - - const search = session.getSearch(); - let home_dir = (process.env as any)['SH_HOME_DIR']; + if (!home_dir || typeof home_dir !== 'string' || home_dir.trim() === '') { + throw new Error('Environment variable SH_HOME_DIR is not set or is invalid.'); + } - // Handle based on the test type - switch (test.alias) { - case 'Observe - grab content (text)': - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(`${home_dir}/${test.file}`) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - break; - case 'Observe - grab content (dlt)': - stream - .observe( - new Factory.File() - .type(Factory.FileType.Binary) - .file(`${home_dir}/${test.file}`) - .asDlt({ - filter_config: undefined, - fibex_file_paths: [], - with_storage_header: true, - tz: undefined, - }) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - break; - case 'Observe - grab content (pcapng)': - stream - .observe( - new Factory.File() - .type(Factory.FileType.PcapNG) - .file(`${home_dir}/${test.file}`) - .asDlt({ - filter_config: undefined, - fibex_file_paths: [], - with_storage_header: false, - tz: undefined, - }) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - break; - case 'Stream - startup measurement': + switch (testId) { + case 'test1': + stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(`${home_dir}/${test.file}`) + .get() + .sterilized(), + ) + .catch(finish.bind(null, session, done)); + break; + case 'test2': + stream + .observe( + new Factory.File() + .type(Factory.FileType.Binary) + .file(`${home_dir}/${test.file}`) + .asDlt({ + filter_config: undefined, + fibex_file_paths: [], + with_storage_header: true, + tz: undefined, + }) + .get() + .sterilized(), + ) + .catch(finish.bind(null, session, done)); + break; + case 'test3': + stream + .observe( + new Factory.File() + .type(Factory.FileType.PcapNG) + .file(`${home_dir}/${test.file}`) + .asDlt({ + filter_config: undefined, + fibex_file_paths: [], + with_storage_header: false, + tz: undefined, + }) + .get() + .sterilized(), + ) + .catch(finish.bind(null, session, done)); + break; + case 'test4': const tmpobj1 = createSampleFile( 5000, logger, (i: number) => `some line data: ${i}\n` ); - const startupSession = await Session.create(); - startupSession.debug(true, testName); - - const startupStream = startupSession.getStream(); - if (startupStream instanceof Error) throw startupStream; + let { session: startupSession, stream: startupStream, events: startupEvents, search: startupSearch } = await runners.initializeSession(testName); - const startupEvents = session.getEvents(); - if (startupEvents instanceof Error) throw startupEvents; startupStream.observe( new Factory.Stream() .asText() @@ -128,22 +110,16 @@ describe('Benchmark Tests', function () { ); const startupResults = measurement(); const startupReport = performanceReport(testName, startupResults.ms, test.expectation_ms, `${home_dir}/${test.file}`); - finish(startupSession, done, startupReport ? undefined : new Error(`${testName} is fail`)); + finish([startupSession, session], done, startupReport ? undefined : new Error(`${testName} is fail`)); break; - case 'Stream - shutdown measurement': + case 'test5': const tmpobj2 = createSampleFile( 5000, logger, (i: number) => `some line data: ${i}\n` ); - const shutdownSession = await Session.create(); - shutdownSession.debug(true, testName); - - const shutdownStream = shutdownSession.getStream(); - if (shutdownStream instanceof Error) throw shutdownStream; - - shutdownStream.observe( + stream.observe( new Factory.Stream() .asText() .process({ @@ -154,13 +130,12 @@ describe('Benchmark Tests', function () { .get() .sterilized() ); - const shutdownResults = measurement(); - const shutdownReport = performanceReport(testName, shutdownResults.ms, test.expectation_ms, `${home_dir}/${test.file}`); - finish(shutdownSession, done, shutdownReport ? undefined : new Error(`${testName} is fail`)); + const shutdownResult = measurement(); + const shutdownReport = performanceReport(testName, shutdownResult.ms, test.expectation_ms, `${home_dir}/${test.file}`); + finish(session, done, shutdownReport ? undefined : new Error(`${testName} is fail`)); break; - case 'Stream - Open 50 sessions': - const results = []; - const multiSessions = []; + case 'test6': + const multiSessions = [session]; for (let i = 0; i < 50; i++) { const file = createSampleFile( 100, @@ -168,15 +143,10 @@ describe('Benchmark Tests', function () { (j: number) => `file ${i} line data: ${j}\n` ); - const multiSession = await Session.create(); + let { session: multiSession, stream: multiSessionStream, events: multiSessionEvents, search: multiSessionSearch } = await runners.initializeSession(testName); multiSessions.push(multiSession); - const multiStream = multiSession.getStream(); - if (multiStream instanceof Error) { - throw multiStream; - } - - let result = multiStream.observe( + multiSessionStream.observe( new Factory.Stream() .asText() .process({ @@ -186,14 +156,13 @@ describe('Benchmark Tests', function () { }) .get() .sterilized() - ).catch((err) => `File ${i} failed to open: ${err.message}`); - results.push(result); + ).catch((err: Error) => `File ${i} failed to open: ${err.message}`); } - const multiResults = measurement(); - const multiReport = performanceReport(testName, multiResults.ms, test.expectation_ms, `${home_dir}/${test.file}`); - finish(undefined, done, multiReport ? undefined : new Error(`${testName} is fail`)); + const testResult = measurement(); + const testReport = performanceReport(testName, testResult.ms, test.expectation_ms, `${home_dir}/${test.file}`); + finish(multiSessions, done, testReport ? undefined : new Error(`${testName} is fail`)); break; - case 'Indexes - Switch to breadcrumb mode': + case 'test7': let controlSum = 0; let countMatches = 0; let read: boolean = false; @@ -207,7 +176,7 @@ describe('Benchmark Tests', function () { ) .catch(finish.bind(null, session, done)); const updates: number[] = []; - events.IndexedMapUpdated.subscribe((event) => { + events.IndexedMapUpdated.subscribe((event: any) => { event.len > 0 && updates.push(event.len); }); events.StreamUpdated.subscribe(async () => { @@ -235,58 +204,58 @@ describe('Benchmark Tests', function () { } }); break; - case 'Assign & single search': - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(`${home_dir}/${test.file}`) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'http', - flags: { reg: true, word: false, cases: false }, - }, - ]) - .catch(finish.bind(null, session, done)); - }) + case 'test8': + stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(`${home_dir}/${test.file}`) + .get() + .sterilized(), + ) + .on('processing', () => { + search + .search([ + { + filter: 'http', + flags: { reg: true, word: false, cases: false }, + }, + ]) .catch(finish.bind(null, session, done)); - break; - case 'Assign & multiple search': - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(`${home_dir}/${test.file}`) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'http://www.almhuette-raith.at', - flags: { reg: true, word: false, cases: false }, - }, - { - filter: 'com.apple.hiservices-xpcservice', - flags: { reg: true, word: false, cases: false }, - }, - { - filter: 'Google Chrome Helper', - flags: { reg: true, word: false, cases: false }, - }, - ]) - .catch(finish.bind(null, session, done)); - }) + }) + .catch(finish.bind(null, session, done)); + break; + case 'test9': + stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(`${home_dir}/${test.file}`) + .get() + .sterilized(), + ) + .on('processing', () => { + search + .search([ + { + filter: 'http://www.almhuette-raith.at', + flags: { reg: true, word: false, cases: false }, + }, + { + filter: 'com.apple.hiservices-xpcservice', + flags: { reg: true, word: false, cases: false }, + }, + { + filter: 'Google Chrome Helper', + flags: { reg: true, word: false, cases: false }, + }, + ]) .catch(finish.bind(null, session, done)); - break; + }) + .catch(finish.bind(null, session, done)); + break; default: throw new Error(`Unsupported format or alias: ${test.alias}`); } @@ -295,10 +264,6 @@ describe('Benchmark Tests', function () { const reportResult = performanceReport(testName, results.ms, test.expectation_ms, `${home_dir}/${test.file}`); finish(session, done, reportResult ? undefined : new Error(`${testName} is fail`)); }); - - } catch (err) { - finish(undefined, done, new Error(`Failed to complete test "${testName}" due to error: ${err instanceof Error ? err.message : err}`)); - } }, ); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.cancel.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.cancel.spec.ts index 77acb7e82..70a21d169 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.cancel.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.cancel.spec.ts @@ -3,83 +3,56 @@ // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; -import { finish, runner } from './common'; +import { Factory } from '../src/api/session'; +import { finish } from './common'; import { readConfigurationFile } from './config'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.cancel; describe('Cancel', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[1]); - let sleep = session - .sleep(2000, false) - .then((results) => { - finish(session, done, new Error(`Operation isn't canceled`)); - }) - .catch((err: Error) => { - finish(session, done, err); - }) - .canceled((reason) => { - finish(session, done); - }); - setTimeout(() => { - sleep.abort(); - }, 250); + return runners.withSession(config.regular, 1, async (_logger, done, comps) => { + let sleep = comps.session + .sleep(2000, false) + .then((results) => { + finish(comps.session, done, new Error(`Operation isn't canceled`)); }) .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); + finish(comps.session, done, err); + }) + .canceled((reason) => { + finish(comps.session, done); }); + setTimeout(() => { + sleep.abort(); + }, 250); }); }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[2]); - let sleep = session - .sleep(250, false) - .then((results) => { - finish(session, done); - }) - .catch((err: Error) => { - finish(session, done, err); - }) - .canceled((reason) => { - finish(session, done, new Error(`Operation cannot be canceled`)); - }); - setTimeout(() => { - sleep.abort(); - }, 1000); + return runners.withSession(config.regular, 2, async (_logger, done, comps) => { + let sleep = comps.session + .sleep(250, false) + .then((results) => { + finish(comps.session, done); }) .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); + finish(comps.session, done, err); + }) + .canceled((reason) => { + finish(comps.session, done, new Error(`Operation cannot be canceled`)); }); + setTimeout(() => { + sleep.abort(); + }, 1000); }); }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { + return runners.withSession(config.regular, 3, async (_logger, done, comps) => { if (config.regular.files === undefined || config.regular.files['text'] === undefined) { return finish( undefined, @@ -99,141 +72,110 @@ describe('Cancel', function () { ); } const spec = config.regular.spec.cancel[3]; - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[3]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - if (search instanceof Error) { - finish(session, done, search); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - stream - .observe( - new Factory.File() - .file(config.regular.files['text']) - .type(Factory.FileType.Text) - .asText() - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let canceled: number = 0; - let started: number = 0; - let processed: number = 0; - let resolved: number = 0; - const errors: Map = new Map(); - const searches = spec.terms.length; - const check_errors = (): boolean => { - if (started !== searches) { - finish( - session, - done, - new Error( - `Total amount of search-requests ${searches}. Started - ${started}; Expectation: ${searches}`, - ), - ); - return false; - } - if (canceled !== searches - 1) { - finish( - session, - done, - new Error( - `Total amount of search-requests ${searches}. Canceled - ${canceled}; Expectation: ${ - searches - 1 - }`, - ), - ); - return false; - } - if (errors.size !== 0) { - errors.forEach((error: Error) => { - console.log(`Error: ${error.message}`); - }); - finish(session, done, new Error(`Has errors during searching`)); - return false; - } - return true; - }; - const runner = (terms: string[], index: number) => { - if (terms.length === 0) { - return; - } - let filter = terms.splice(0, 1)[0]; - started += 1; - let task = search - .search([ - { - filter, - flags: { reg: true, word: false, cases: false }, - }, - ]) - .canceled(() => { - console.log( - `-> [..🗑..]\tsearch operation #${index} (${task.uuid()}) is canceled`, - ); - canceled += 1; - }) - .then(() => { - console.log( - `-> [..👝..]\tsearch operation #${index} (${task.uuid()}) is resolved`, - ); - resolved += 1; - }) - .catch((err: Error) => { - console.log( - `-> [..⚠..]\tsearch operation #${index} (${task.uuid()}) triggers error`, - ); - errors.set(index, err); - }) - .finally(() => { - processed += 1; - if (processed === searches) { - if (!check_errors()) { - // Failed - return; - } - if (resolved === 0) { - return finish( - session, - done, - new Error(`At least one search should resolved`), - ); - } - finish(session, done); - } - }); - setTimeout(() => { - runner(terms, index + 1); - }, spec.interval_ms); - }; - console.log(`Waiting until target file would be indexed.`); - events.FileRead.subscribe(() => { - runner(spec.terms, 1); - }); - }) - .catch((err: Error) => { + comps.stream + .observe( + new Factory.File() + .file(config.regular.files['text']) + .type(Factory.FileType.Text) + .asText() + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let canceled: number = 0; + let started: number = 0; + let processed: number = 0; + let resolved: number = 0; + const errors: Map = new Map(); + const searches = spec.terms.length; + const check_errors = (): boolean => { + if (started !== searches) { + finish( + comps.session, + done, + new Error( + `Total amount of search-requests ${searches}. Started - ${started}; Expectation: ${searches}`, + ), + ); + return false; + } + if (canceled !== searches - 1) { finish( - undefined, + comps.session, done, new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err + `Total amount of search-requests ${searches}. Canceled - ${canceled}; Expectation: ${ + searches - 1 }`, ), ); - }); + return false; + } + if (errors.size !== 0) { + errors.forEach((error: Error) => { + console.log(`Error: ${error.message}`); + }); + finish(comps.session, done, new Error(`Has errors during searching`)); + return false; + } + return true; + }; + const runner = (terms: string[], index: number) => { + if (terms.length === 0) { + return; + } + let filter = terms.splice(0, 1)[0]; + started += 1; + let task = comps.search + .search([ + { + filter, + flags: { reg: true, word: false, cases: false }, + }, + ]) + .canceled(() => { + console.log( + `-> [..🗑..]\tsearch operation #${index} (${task.uuid()}) is canceled`, + ); + canceled += 1; + }) + .then(() => { + console.log( + `-> [..👝..]\tsearch operation #${index} (${task.uuid()}) is resolved`, + ); + resolved += 1; + }) + .catch((err: Error) => { + console.log( + `-> [..⚠..]\tsearch operation #${index} (${task.uuid()}) triggers error`, + ); + errors.set(index, err); + }) + .finally(() => { + processed += 1; + if (processed === searches) { + if (!check_errors()) { + // Failed + return; + } + if (resolved === 0) { + return finish( + comps.session, + done, + new Error(`At least one search should resolved`), + ); + } + finish(comps.session, done); + } + }); + setTimeout(() => { + runner(terms, index + 1); + }, spec.interval_ms); + }; + console.log(`Waiting until target file would be indexed.`); + comps.events.FileRead.subscribe(() => { + runner(spec.terms, 1); + }); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.concat.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.concat.spec.ts index 0825a8324..c63177295 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.concat.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.concat.spec.ts @@ -1,99 +1,77 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IGrabbedElement } from 'platform/types/content'; -import { createSampleFile, finish, runner } from './common'; +import { createSampleFile, finish } from './common'; import { readConfigurationFile } from './config'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.concat; describe('Concat', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - const tmpobj_a = createSampleFile( - 100, - logger, - (i: number) => `file a: some line data: ${i}\n`, - ); - const tmpobj_b = createSampleFile( - 100, - logger, - (i: number) => `file b: some line data: ${i}\n`, - ); - stream - .observe( - new Factory.Concat() - .type(Factory.FileType.Text) - .files([tmpobj_a.name, tmpobj_b.name]) - .asText().get().sterilized(), - ) - .catch(finish.bind(null, session, done)); - let grabbing: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 120 || grabbing) { - return; - } - grabbing = true; - stream - .grab(98, 4) - .then((result: IGrabbedElement[]) => { - logger.debug('result of grab was: ' + JSON.stringify(result)); - expect(result.map((i) => i.content)).toEqual([ - 'file a: some line data: 98', - 'file a: some line data: 99', - 'file b: some line data: 0', - 'file b: some line data: 1', - ]); - finish(session, done); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to grab data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + return runners.withSession(config.regular, 1, async (logger, done, comps) => { + const tmpobj_a = createSampleFile( + 100, + logger, + (i: number) => `file a: some line data: ${i}\n`, + ); + const tmpobj_b = createSampleFile( + 100, + logger, + (i: number) => `file b: some line data: ${i}\n`, + ); + comps.stream + .observe( + new Factory.Concat() + .type(Factory.FileType.Text) + .files([tmpobj_a.name, tmpobj_b.name]) + .asText() + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let grabbing: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 120 || grabbing) { + return; + } + grabbing = true; + comps.stream + .grab(98, 4) + .then((result: IGrabbedElement[]) => { + logger.debug('result of grab was: ' + JSON.stringify(result)); + expect(result.map((i) => i.content)).toEqual([ + 'file a: some line data: 98', + 'file a: some line data: 99', + 'file b: some line data: 0', + 'file b: some line data: 1', + ]); + finish(comps.session, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to grab data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + }); }); }); // it(config.regular.list[2], function () { - // return runner(config.regular, 2, async (logger, done, collector) => { + // return runners.withSession(config.regular, 2, async (logger, done, collector) => { // Session.create() // .then((session: Session) => { // // Set provider into debug mode @@ -172,7 +150,7 @@ describe('Concat', function () { // }); // it(config.regular.list[3], function () { - // return runner(config.regular, 3, async (logger, done, collector) => { + // return runners.withSession(config.regular, 3, async (logger, done, collector) => { // Session.create() // .then((session: Session) => { // // Set provider into debug mode diff --git a/application/apps/rustcore/ts-bindings/spec/session.errors.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.errors.spec.ts index f4776bbdf..9569da97f 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.errors.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.errors.spec.ts @@ -1,524 +1,335 @@ // tslint:disable +// We need to provide path to TypeScript types definitions +/// +/// + import { initLogger } from './logger'; initLogger(); -import { Session, SessionStream, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IGrabbedElement } from 'platform/types/content'; -import { finish, createSampleFile, runner } from './common'; +import { finish, createSampleFile } from './common'; import { readConfigurationFile } from './config'; import { error } from 'platform/log/utils'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.errors; describe('Errors', () => { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[1]); - const search = session.getSearch(); - if (search instanceof Error) { - return finish(session, done, search); - } - search - .search([ - { - filter: 'match', - flags: { reg: true, word: true, cases: false }, - }, - ]) - .then((found: number) => - finish(session, done, new Error('Search should not be available')), - ) - .catch((err: Error) => { - logger.debug(`Expected error: ${err.message}`); - finish(session, done); - }); - }) + return runners.withSession(config.regular, 1, async (logger, done, comps) => { + comps.search + .search([ + { + filter: 'match', + flags: { reg: true, word: true, cases: false }, + }, + ]) + .then((_found: number) => + finish(comps.session, done, new Error('Search should not be available')), + ) .catch((err: Error) => { - finish( - undefined, - done, - new Error(`Fail to create session due error: ${error(err)}`), - ); + logger.debug(`Expected error: ${err.message}`); + finish(comps.session, done); }); }); }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[2]); - const stream = session.getStream(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - stream - .observe( - new Factory.File() - .type(Factory.FileType.Text) - .asText() - .file('/fake/path/to/fake/file') - .get() - .sterilized(), - ) - .then( - finish.bind( - null, - session, - done, - new Error(`Not exist file cannot be opened`), - ), - ) - .catch((err: Error) => { - logger.debug(`Expected error: ${err.message}`); - finish(session, done); - }); - }) - .catch((err: Error) => { - finish( - undefined, + return runners.withSession(config.regular, 2, async (logger, done, comps) => { + comps.stream + .observe( + new Factory.File() + .type(Factory.FileType.Text) + .asText() + .file('/fake/path/to/fake/file') + .get() + .sterilized(), + ) + .then( + finish.bind( + null, + comps.session, done, - new Error(`Fail to create session due error: ${error(err)}`), - ); + new Error(`Not exist file cannot be opened`), + ), + ) + .catch((err: Error) => { + logger.debug(`Expected error: ${err.message}`); + finish(comps.session, done); }); }); }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[3]); - const stream = session.getStream(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - const events = session.getEvents(); - if (events instanceof Error) { - return finish(session, done, events); - } - const tmpobj = createSampleFile( - 5000, - logger, - (i: number) => `some line data: ${i}\n`, - ); - stream - .observe( - new Factory.File() - .type(Factory.FileType.Text) - .asText() - .file(tmpobj.name) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let grabbing: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows === 0 || grabbing) { - return; - } - grabbing = true; - // While we do not have operation id - stream - .grab(6000, 1000) - .then((result: IGrabbedElement[]) => { - finish( - session, - done, - new Error(`grabber should not return results`), - ); - }) - .catch((err: Error) => { - logger.debug(`Expected error: ${err.message}`); - finish(session, done); - }); + return runners.withSession(config.regular, 3, async (logger, done, comps) => { + const tmpobj = createSampleFile(5000, logger, (i: number) => `some line data: ${i}\n`); + comps.stream + .observe( + new Factory.File() + .type(Factory.FileType.Text) + .asText() + .file(tmpobj.name) + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let grabbing: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows === 0 || grabbing) { + return; + } + grabbing = true; + // While we do not have operation id + comps.stream + .grab(6000, 1000) + .then((_result: IGrabbedElement[]) => { + finish(comps.session, done, new Error(`grabber should not return results`)); + }) + .catch((err: Error) => { + logger.debug(`Expected error: ${err.message}`); + finish(comps.session, done); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error(`Fail to create session due error: ${error(err)}`), - ); - }); + }); }); }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[4]); - const stream: SessionStream = session.getStream(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - const events = session.getEvents(); - if (events instanceof Error) { - return finish(session, done, events); - } - const search = session.getSearch(); - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - 5000, - logger, - (i: number) => - `[${i}]:: ${ - i % 100 === 0 || i <= 5 - ? `some match line data\n` - : `some line data\n` - }`, - ); - stream - .observe( - new Factory.File() - .type(Factory.FileType.Text) - .asText() - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'match', - flags: { reg: true, word: false, cases: false }, - }, - ]) - .then((found: number) => { - search - .len() - .then((len: number) => { - expect(len).toEqual(55); - search - .grab(6000, 1000) - .then((result: IGrabbedElement[]) => { - finish( - session, - done, - new Error( - `search grabber should not return results`, - ), - ); - }) - .catch((err: Error) => { - logger.debug(`Expected error: ${err.message}`); - finish(session, done); - }); + return runners.withSession(config.regular, 4, async (logger, done, comps) => { + const tmpobj = createSampleFile( + 5000, + logger, + (i: number) => + `[${i}]:: ${ + i % 100 === 0 || i <= 5 ? `some match line data\n` : `some line data\n` + }`, + ); + comps.stream + .observe( + new Factory.File() + .type(Factory.FileType.Text) + .asText() + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: 'match', + flags: { reg: true, word: false, cases: false }, + }, + ]) + .then((found: number) => { + comps.search + .len() + .then((len: number) => { + expect(len).toEqual(55); + comps.search + .grab(6000, 1000) + .then((result: IGrabbedElement[]) => { + finish( + comps.session, + done, + new Error( + `search grabber should not return results`, + ), + ); }) .catch((err: Error) => { - finish(session, done, err); + logger.debug(`Expected error: ${err.message}`); + finish(comps.session, done); }); }) - .catch(finish.bind(null, session, done)); + .catch((err: Error) => { + finish(comps.session, done, err); + }); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error(`Fail to create session due error: ${error(err)}`), - ); - }); + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[5]); - const stream: SessionStream = session.getStream(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - const tmpobj = createSampleFile( - 5, - logger, - (i: number) => `some line data: ${i}\n`, - ); - stream - .observe( - new Factory.File() - .type(Factory.FileType.Text) - .asText() - .file(tmpobj.name) - .get() - .sterilized(), - ) - .catch((err: Error) => { - finish( - session, - done, - new Error(`Failed to observe file: ${err.message}`), - ); - }); - let grabbing: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows === 0 || grabbing) { - return; - } - stream - .grab(1, -2) - .then((result: IGrabbedElement[]) => { - finish( - session, - done, - new Error('Grab from invalid range should not work'), - ); - }) - .catch((err: Error) => { - logger.debug(`Expected error: ${err.message}`); - finish(session, done); - }); - }); - }) + return runners.withSession(config.regular, 5, async (logger, done, comps) => { + const tmpobj = createSampleFile(5, logger, (i: number) => `some line data: ${i}\n`); + comps.stream + .observe( + new Factory.File() + .type(Factory.FileType.Text) + .asText() + .file(tmpobj.name) + .get() + .sterilized(), + ) .catch((err: Error) => { finish( - undefined, + comps.session, done, - new Error(`Fail to create session due error: ${error(err)}`), + new Error(`Failed to observe file: ${err.message}`), ); }); + let grabbing: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows === 0 || grabbing) { + return; + } + comps.stream + .grab(1, -2) + .then((_result: IGrabbedElement[]) => { + finish( + comps.session, + done, + new Error('Grab from invalid range should not work'), + ); + }) + .catch((err: Error) => { + logger.debug(`Expected error: ${err.message}`); + finish(comps.session, done); + }); + }); }); }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[6]); - const stream: SessionStream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - const tmpobj = createSampleFile( - 5, - logger, - (i: number) => `some line data: ${i}\n`, - ); - stream - .observe( - new Factory.File() - .type(Factory.FileType.Text) - .asText() - .file(tmpobj.name) - .get() - .sterilized(), - ) - .catch((err: Error) => - finish( - session, - done, - new Error(`Failed to observe file: ${err.message}`), - ), - ); - let grabbing: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows === 0 || grabbing) { - return; - } - grabbing = true; - stream - .grab(-1, 2) - .then((result: IGrabbedElement[]) => - finish(session, done, new Error('Grab from invalid start worked')), - ) - .catch((err: Error) => { - logger.debug(`Expected error: ${err.message}`); - finish(session, done); - }); - }); - }) - .catch((err: Error) => { + return runners.withSession(config.regular, 6, async (logger, done, comps) => { + const tmpobj = createSampleFile(5, logger, (i: number) => `some line data: ${i}\n`); + comps.stream + .observe( + new Factory.File() + .type(Factory.FileType.Text) + .asText() + .file(tmpobj.name) + .get() + .sterilized(), + ) + .catch((err: Error) => finish( - undefined, + comps.session, done, - new Error(`Fail to create session due error: ${error(err)}`), - ); - }); + new Error(`Failed to observe file: ${err.message}`), + ), + ); + let grabbing: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows === 0 || grabbing) { + return; + } + grabbing = true; + comps.stream + .grab(-1, 2) + .then((_result: IGrabbedElement[]) => + finish(comps.session, done, new Error('Grab from invalid start worked')), + ) + .catch((err: Error) => { + logger.debug(`Expected error: ${err.message}`); + finish(comps.session, done); + }); + }); }); }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[7]); - session.getEvents().SessionDestroyed.subscribe(() => { - finish(undefined, done); - }); - session - .getNativeSession() - .triggerStateError() - .catch((err: Error) => { - finish( - session, - done, - new Error(`Fail to trigger state error due error: ${error(err)}`), - ); - }); - }) + return runners.withSession(config.regular, 7, async (logger, done, comps) => { + comps.events.SessionDestroyed.subscribe(() => { + finish(undefined, done); + }); + comps.session + .getNativeSession() + .triggerStateError() .catch((err: Error) => { finish( - undefined, + comps.session, done, - new Error(`Fail to create session due error: ${error(err)}`), + new Error(`Fail to trigger state error due error: ${error(err)}`), ); }); }); }); it(config.regular.list[8], function () { - return runner(config.regular, 8, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[8]); - session.getEvents().SessionDestroyed.subscribe(() => { - finish(undefined, done); - }); - session - .getNativeSession() - .triggerTrackerError() - .catch((err: Error) => { - finish( - session, - done, - new Error(`Fail to trigger tracker error due error: ${error(err)}`), - ); - }); - }) + return runners.withSession(config.regular, 8, async (logger, done, comps) => { + comps.events.SessionDestroyed.subscribe(() => { + finish(undefined, done); + }); + comps.session + .getNativeSession() + .triggerTrackerError() .catch((err: Error) => { finish( - undefined, + comps.session, done, - new Error(`Fail to create session due error: ${error(err)}`), + new Error(`Fail to trigger tracker error due error: ${error(err)}`), ); }); }); }); it(config.regular.list[9], function () { - return runner(config.regular, 9, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[9]); - session - .sleep(10000, true) - .then(() => { - finish(session, done, new Error(`Sleeping task should not finish.`)); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error(`Fail to start sleeping task: ${err.message}`), - ); - }); - setTimeout(() => { - session - .destroy() - .then(() => { - finish(undefined, done); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error(`Fail to destroy session: ${err.message}`), - ); - }); - }, 500); + return runners.withSession(config.regular, 9, async (logger, done, comps) => { + comps.session + .sleep(10000, true) + .then(() => { + finish(comps.session, done, new Error(`Sleeping task should not finish.`)); }) .catch((err: Error) => { finish( - undefined, + comps.session, done, - new Error(`Fail to create session due error: ${error(err)}`), + new Error(`Fail to start sleeping task: ${err.message}`), ); }); + setTimeout(() => { + comps.session + .destroy() + .then(() => { + finish(undefined, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error(`Fail to destroy session: ${err.message}`), + ); + }); + }, 500); }); }); it(config.regular.list[10], function () { - return runner(config.regular, 10, async (logger, done, _collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[10]); - const stream = session.getStream(); - const search = session.getSearch(); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - 5000, - logger, - (i: number) => - `[${i}]:: ${ - i % 100 === 0 || i <= 5 - ? `some match line data\n` - : `some line data\n` - }`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'invalid search { condition', - flags: { reg: true, word: false, cases: false }, - }, - ]) - .then((_) => { - finish(session, done, new Error(`Search should be failed`)); - }) - .catch((_err: Error) => { - finish(session, done); - }); + return runners.withSession(config.regular, 10, async (logger, done, comps) => { + const tmpobj = createSampleFile( + 5000, + logger, + (i: number) => + `[${i}]:: ${ + i % 100 === 0 || i <= 5 ? `some match line data\n` : `some line data\n` + }`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: 'invalid search { condition', + flags: { reg: true, word: false, cases: false }, + }, + ]) + .then((_) => { + finish(comps.session, done, new Error(`Search should be failed`)); }) - .catch(finish.bind(null, session, done)); + .catch((_err: Error) => { + finish(comps.session, done); + }); }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + .catch(finish.bind(null, comps.session, done)); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.exporting.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.exporting.spec.ts index 0cee500fe..2e771eae6 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.exporting.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.exporting.spec.ts @@ -1,13 +1,13 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IGrabbedElement } from 'platform/types/content'; -import { createSampleFile, finish, runner } from './common'; +import { createSampleFile, finish } from './common'; import { readConfigurationFile } from './config'; import { fromIndexes } from 'platform/types/range'; import { v4 } from 'uuid'; @@ -15,733 +15,543 @@ import { v4 } from 'uuid'; import * as os from 'os'; import * as fs from 'fs'; import * as path from 'path'; +import * as runners from './runners'; const config = readConfigurationFile().get().tests.exporting; describe('Exporting', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); + return runners.withSession(config.regular, 1, async (logger, done, comps) => { + let controlSum = 0; + const ranges = [ + { + from: 50, + to: 100, + }, + { + from: 200, + to: 300, + }, + ]; + const tmpobj = createSampleFile(1000, logger, (i: number) => { + ranges.forEach((r) => { + if (i >= r.from && i <= r.to) { + controlSum += i; + } + }); + return `____${i}____\n`; + }); + comps.stream + .observe( + new Factory.File() + .type(Factory.FileType.Text) + .asText() + .file(tmpobj.name) + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let gotten: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 500 || gotten) { return; } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); + gotten = true; + const output = path.resolve(os.tmpdir(), `${v4()}.logs`); + comps.stream + .export(output, ranges) + .then(() => { + fs.promises + .readFile(output, { encoding: 'utf-8' }) + .then((content) => { + const rows = content + .split('\n') + .map((r) => parseInt(r.replace(/_/gi, ''), 10)); + const sum = rows.reduce((partialSum, a) => partialSum + a, 0); + expect(sum).toEqual(controlSum); + finish(comps.session, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to read output file due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }) + .finally(() => { + fs.unlinkSync(output); + }); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to export data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }); + }); + }); + + it(config.regular.list[2], function () { + return runners.withSession(config.regular, 2, async (logger, done, comps) => { + const ranges = [ + { + from: 50, + to: 90, + }, + { + from: 101, + to: 150, + }, + ]; + let controlSum = 0; + const tmpobj_a = createSampleFile(100, logger, (i: number) => { + if (i >= ranges[0].from && i <= ranges[0].to) { + controlSum += i; + } + return `____${i}____\n`; + }); + const tmpobj_b = createSampleFile(100, logger, (i: number) => { + if (i >= ranges[1].from - 100 && i <= ranges[1].to - 100) { + controlSum += i * 1000; + } + return `____${i * 1000}____\n`; + }); + comps.stream + .observe( + new Factory.Concat() + .type(Factory.FileType.Text) + .files([tmpobj_a.name, tmpobj_b.name]) + .asText() + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let gotten: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 199 || gotten) { return; } - let controlSum = 0; - const ranges = [ - { - from: 50, - to: 100, - }, - { - from: 200, - to: 300, - }, - ]; - const tmpobj = createSampleFile(1000, logger, (i: number) => { - ranges.forEach((r) => { - if (i >= r.from && i <= r.to) { - controlSum += i; - } + gotten = true; + const output = path.resolve(os.tmpdir(), `${v4()}.logs`); + comps.stream + .export(output, ranges) + .then(() => { + fs.promises + .readFile(output, { encoding: 'utf-8' }) + .then((content) => { + const rows = content + .split('\n') + .map((r) => parseInt(r.replace(/_/gi, ''), 10)); + const sum = rows.reduce((partialSum, a) => partialSum + a, 0); + expect(sum).toEqual(controlSum); + finish(comps.session, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to read output file due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }) + .finally(() => { + fs.unlinkSync(output); + }); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to export data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); }); - return `____${i}____\n`; - }); - stream + }); + }); + }); + + it(config.regular.list[3], function () { + return runners.withSession(config.regular, 3, async (logger, done, comps) => { + let controlSum = 0; + const range = { + from: 0, + to: 50, + }; + let countOfMatches = 0; + const tmpobj = createSampleFile(5000, logger, (i: number) => { + if (i % 100 === 0 || i <= 5) { + if (countOfMatches < range.to) { + controlSum += i; + } + countOfMatches += 1; + } + return `${i % 100 === 0 || i <= 5 ? `_____${i}_____\n` : `some line data\n`}`; + }); + comps.stream + .observe( + new Factory.File() + .type(Factory.FileType.Text) + .asText() + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: '__\\d+__', + flags: { reg: true, word: false, cases: false }, + }, + ]) + .then((_found) => { + const output = path.resolve(os.tmpdir(), `${v4()}.logs`); + comps.search + .grab(range.from, range.to) + .then((grabbed: IGrabbedElement[]) => { + comps.stream + .export( + output, + fromIndexes(grabbed.map((el) => el.position)), + ) + .then((_done) => { + fs.promises + .readFile(output, { encoding: 'utf-8' }) + .then((content) => { + const rows = content + .split('\n') + .map((r) => + parseInt(r.replace(/_/gi, ''), 10), + ); + expect(grabbed.length).toEqual(rows.length); + const sum = rows.reduce( + (partialSum, a) => partialSum + a, + 0, + ); + expect(sum).toEqual(controlSum); + finish(comps.session, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to read output file due error: ${ + err instanceof Error + ? err.message + : err + }`, + ), + ); + }) + .finally(() => { + fs.unlinkSync(output); + }); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to export data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to grab data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }); + }); + + if ( + config.regular.files['dlt'] === undefined || + config.regular.files['dlt'].length < 1 || + !fs.existsSync(config.regular.files['dlt'][0]) + ) { + console.log( + `"${config.regular.list[4]}" has been ignored - fail to find DLT file for testing`, + ); + console.log( + `"${config.regular.list[5]}" has been ignored - fail to find DLT file for testing`, + ); + } else { + it(config.regular.list[4], function () { + return runners.withSession(config.regular, 4, async (logger, done, comps) => { + const configuration = new Factory.File() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .file(config.regular.files['dlt'][0]) + .get(); + console.log(configuration); + comps.stream .observe( new Factory.File() - .type(Factory.FileType.Text) - .asText() - .file(tmpobj.name) + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .file(config.regular.files['dlt'][0]) .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let gotten: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 500 || gotten) { + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 400 || gotten) { return; } gotten = true; - const output = path.resolve(os.tmpdir(), `${v4()}.logs`); - stream - .export(output, ranges) - .then(() => { - fs.promises - .readFile(output, { encoding: 'utf-8' }) - .then((content) => { - const rows = content - .split('\n') - .map((r) => parseInt(r.replace(/_/gi, ''), 10)); - const sum = rows.reduce( - (partialSum, a) => partialSum + a, - 0, - ); - expect(sum).toEqual(controlSum); - finish(session, done); + comps.stream + .grab(100, 100) + .then((grabbed) => { + const output = path.resolve(os.tmpdir(), `${v4()}.logs`); + comps.stream + .export(output, [{ from: 100, to: 199 }]) + .then(() => { + fs.promises + .readFile(output, { encoding: 'utf-8' }) + .then((content) => { + const rows = content.split('\n'); + expect(rows.length).toEqual(grabbed.length); + for (let i = 0; i < rows.length; i += 1) { + expect(rows[i]).toEqual(grabbed[i].content); + if (rows[i] !== grabbed[i].content) { + console.log( + `Rows are dismatch. Stream position ${grabbed[i].position}.`, + ); + return finish(comps.session, done); + } + } + finish(comps.session, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to read output file due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }) + .finally(() => { + fs.unlinkSync(output); + }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( - `Fail to read output file due error: ${ + `Fail to export data due error: ${ err instanceof Error ? err.message : err }`, ), ); - }) - .finally(() => { - fs.unlinkSync(output); }); }) .catch((err: Error) => { finish( - session, + undefined, done, new Error( - `Fail to export data due error: ${ + `Fail to grab due error: ${ err instanceof Error ? err.message : err }`, ), ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + }); }); - }); - it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - const ranges = [ - { - from: 50, - to: 90, - }, - { - from: 101, - to: 150, - }, - ]; - let controlSum = 0; - const tmpobj_a = createSampleFile(100, logger, (i: number) => { - if (i >= ranges[0].from && i <= ranges[0].to) { - controlSum += i; - } - return `____${i}____\n`; - }); - const tmpobj_b = createSampleFile(100, logger, (i: number) => { - if (i >= ranges[1].from - 100 && i <= ranges[1].to - 100) { - controlSum += i * 1000; - } - return `____${i * 1000}____\n`; - }); - stream + it(config.regular.list[5], function () { + return runners.withSession(config.regular, 5, async (logger, done, comps) => { + comps.stream .observe( - new Factory.Concat() - .type(Factory.FileType.Text) - .files([tmpobj_a.name, tmpobj_b.name]) - .asText() + new Factory.File() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .file(config.regular.files['dlt'][0]) .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let gotten: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 199 || gotten) { + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 400 || gotten) { return; } gotten = true; - const output = path.resolve(os.tmpdir(), `${v4()}.logs`); - stream - .export(output, ranges) - .then(() => { - fs.promises - .readFile(output, { encoding: 'utf-8' }) - .then((content) => { - const rows = content - .split('\n') - .map((r) => parseInt(r.replace(/_/gi, ''), 10)); - const sum = rows.reduce( - (partialSum, a) => partialSum + a, - 0, - ); - expect(sum).toEqual(controlSum); - finish(session, done); + comps.stream + .grab(100, 100) + .then((grabbed) => { + const output = path.resolve(os.tmpdir(), `${v4()}.dlt`); + comps.stream + .exportRaw(output, [{ from: 100, to: 199 }]) + .then(async () => { + comps.session + .destroy() + .then(async () => { + const { session, stream, search, events } = + await runners.initializeSession( + config.regular.list[5], + ); + stream + .observe( + new Factory.File() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .file(output) + .get() + .sterilized(), + ) + .catch(finish.bind(null, session, done)); + let gotten: boolean = false; + gotten = false; + events.StreamUpdated.subscribe( + (rows: number) => { + if (rows < 99 || gotten) { + return; + } + gotten = true; + comps.stream + .grab(0, 100) + .then((rows) => { + expect(rows.length).toEqual( + grabbed.length, + ); + for ( + let i = 0; + i < rows.length; + i += 1 + ) { + expect( + rows[i].content, + ).toEqual( + grabbed[i].content, + ); + if ( + rows[i].content !== + grabbed[i].content + ) { + console.log( + `Rows are dismatch. Stream position ${grabbed[i].position}.`, + ); + return finish( + session, + done, + ); + } + } + finish(session, done); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to grab due error: ${ + err instanceof Error + ? err.message + : err + }`, + ), + ); + }); + }, + ); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to destroy session due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( - `Fail to read output file due error: ${ + `Fail to export data due error: ${ err instanceof Error ? err.message : err }`, ), ); - }) - .finally(() => { - fs.unlinkSync(output); }); }) .catch((err: Error) => { finish( - session, + undefined, done, new Error( - `Fail to export data due error: ${ + `Fail to grab due error: ${ err instanceof Error ? err.message : err }`, ), ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }); - - it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[3]); - const stream = session.getStream(); - const search = session.getSearch(); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - let controlSum = 0; - const range = { - from: 0, - to: 50, - }; - let countOfMatches = 0; - const tmpobj = createSampleFile(5000, logger, (i: number) => { - if (i % 100 === 0 || i <= 5) { - if (countOfMatches < range.to) { - controlSum += i; - } - countOfMatches += 1; - } - return `${ - i % 100 === 0 || i <= 5 ? `_____${i}_____\n` : `some line data\n` - }`; - }); - stream - .observe( - new Factory.File() - .type(Factory.FileType.Text) - .asText() - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: '__\\d+__', - flags: { reg: true, word: false, cases: false }, - }, - ]) - .then((_found) => { - const output = path.resolve(os.tmpdir(), `${v4()}.logs`); - search - .grab(range.from, range.to) - .then((grabbed: IGrabbedElement[]) => { - stream - .export( - output, - fromIndexes(grabbed.map((el) => el.position)), - ) - .then((_done) => { - fs.promises - .readFile(output, { encoding: 'utf-8' }) - .then((content) => { - const rows = content - .split('\n') - .map((r) => - parseInt( - r.replace(/_/gi, ''), - 10, - ), - ); - expect(grabbed.length).toEqual( - rows.length, - ); - const sum = rows.reduce( - (partialSum, a) => partialSum + a, - 0, - ); - expect(sum).toEqual(controlSum); - finish(session, done); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to read output file due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }) - .finally(() => { - fs.unlinkSync(output); - }); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to export data due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to grab data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }); - - if ( - config.regular.files['dlt'] === undefined || - config.regular.files['dlt'].length < 1 || - !fs.existsSync(config.regular.files['dlt'][0]) - ) { - console.log( - `"${config.regular.list[4]}" has been ignored - fail to find DLT file for testing`, - ); - console.log( - `"${config.regular.list[5]}" has been ignored - fail to find DLT file for testing`, - ); - } else { - it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - const configuration = new Factory.File() - .type(Factory.FileType.Binary) - .asDlt({ - fibex_file_paths: [], - filter_config: undefined, - with_storage_header: true, - tz: undefined, - }) - .file(config.regular.files['dlt'][0]) - .get(); - console.log(configuration); - stream - .observe( - new Factory.File() - .type(Factory.FileType.Binary) - .asDlt({ - fibex_file_paths: [], - filter_config: undefined, - with_storage_header: true, - tz: undefined, - }) - .file(config.regular.files['dlt'][0]) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let gotten: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 400 || gotten) { - return; - } - gotten = true; - stream - .grab(100, 100) - .then((grabbed) => { - const output = path.resolve(os.tmpdir(), `${v4()}.logs`); - stream - .export(output, [{ from: 100, to: 199 }]) - .then(() => { - fs.promises - .readFile(output, { encoding: 'utf-8' }) - .then((content) => { - const rows = content.split('\n'); - expect(rows.length).toEqual(grabbed.length); - for (let i = 0; i < rows.length; i += 1) { - expect(rows[i]).toEqual(grabbed[i].content); - if (rows[i] !== grabbed[i].content) { - console.log( - `Rows are dismatch. Stream position ${grabbed[i].position}.`, - ); - return finish(session, done); - } - } - finish(session, done); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to read output file due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }) - .finally(() => { - fs.unlinkSync(output); - }); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to export data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to grab due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }); - - it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - stream - .observe( - new Factory.File() - .type(Factory.FileType.Binary) - .asDlt({ - fibex_file_paths: [], - filter_config: undefined, - with_storage_header: true, - tz: undefined, - }) - .file(config.regular.files['dlt'][0]) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let gotten: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 400 || gotten) { - return; - } - gotten = true; - stream - .grab(100, 100) - .then((grabbed) => { - const output = path.resolve(os.tmpdir(), `${v4()}.dlt`); - stream - .exportRaw(output, [{ from: 100, to: 199 }]) - .then(() => { - session - .destroy() - .then(() => { - Session.create() - .then((session: Session) => { - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - stream - .observe( - new Factory.File() - .type( - Factory.FileType.Binary, - ) - .asDlt({ - fibex_file_paths: [], - filter_config: - undefined, - with_storage_header: - true, - tz: - undefined, - }) - .file(output) - .get() - .sterilized(), - ) - .catch( - finish.bind( - null, - session, - done, - ), - ); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - let gotten: boolean = false; - gotten = false; - events.StreamUpdated.subscribe( - (rows: number) => { - if (rows < 99 || gotten) { - return; - } - gotten = true; - stream - .grab(0, 100) - .then((rows) => { - expect( - rows.length, - ).toEqual( - grabbed.length, - ); - for ( - let i = 0; - i < rows.length; - i += 1 - ) { - expect( - rows[i].content, - ).toEqual( - grabbed[i] - .content, - ); - if ( - rows[i] - .content !== - grabbed[i] - .content - ) { - console.log( - `Rows are dismatch. Stream position ${grabbed[i].position}.`, - ); - return finish( - session, - done, - ); - } - } - finish(session, done); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to grab due error: ${ - err instanceof - Error - ? err.message - : err - }`, - ), - ); - }); - }, - ); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to destroy session due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to export data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to grab due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); } + if ( config.regular.files['dlt'] === undefined || config.regular.files['dlt'].length < 2 || @@ -755,573 +565,417 @@ describe('Exporting', function () { ); } else { it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); + return runners.withSession(config.regular, 6, async (logger, done, comps) => { + comps.stream + .observe( + new Factory.Concat() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .files([ + config.regular.files['dlt'][1], + config.regular.files['dlt'][1], + ]) + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let gotten: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 200 || gotten) { return; } - stream - .observe( - new Factory.Concat() - .type(Factory.FileType.Binary) - .asDlt({ - fibex_file_paths: [], - filter_config: undefined, - with_storage_header: true, - tz: undefined, - }) - .files([ - config.regular.files['dlt'][1], - config.regular.files['dlt'][1], - ]) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let gotten: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 200 || gotten) { - return; - } - gotten = true; - stream - .grab(50, 100) - .then((grabbed) => { - expect(grabbed[54].source_id).toEqual(0); - expect(grabbed[55].source_id).toEqual(1); - const output = path.resolve(os.tmpdir(), `${v4()}.logs`); - stream - .export(output, [{ from: 50, to: 149 }]) - .then(() => { - fs.promises - .readFile(output, { encoding: 'utf-8' }) - .then((content) => { - const rows = content.split('\n'); - expect(rows.length).toEqual(grabbed.length); - for (let i = 0; i < rows.length; i += 1) { - expect(rows[i]).toEqual(grabbed[i].content); - if (rows[i] !== grabbed[i].content) { - console.log( - `Rows are dismatch. Stream position ${grabbed[i].position}.`, - ); - return finish(session, done); - } + gotten = true; + comps.stream + .grab(50, 100) + .then((grabbed) => { + expect(grabbed[54].source_id).toEqual(0); + expect(grabbed[55].source_id).toEqual(1); + const output = path.resolve(os.tmpdir(), `${v4()}.logs`); + comps.stream + .export(output, [{ from: 50, to: 149 }]) + .then(() => { + fs.promises + .readFile(output, { encoding: 'utf-8' }) + .then((content) => { + const rows = content.split('\n'); + expect(rows.length).toEqual(grabbed.length); + for (let i = 0; i < rows.length; i += 1) { + expect(rows[i]).toEqual(grabbed[i].content); + if (rows[i] !== grabbed[i].content) { + console.log( + `Rows are dismatch. Stream position ${grabbed[i].position}.`, + ); + return finish(comps.session, done); } - finish(session, done); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to read output file due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }) - .finally(() => { - fs.unlinkSync(output); - }); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to export data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to grab due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); + } + finish(comps.session, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to read output file due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }) + .finally(() => { + fs.unlinkSync(output); + }); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to export data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to grab due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); }); }); }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); + return runners.withSession(config.regular, 7, async (logger, done, comps) => { + comps.stream + .observe( + new Factory.Concat() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .files([ + config.regular.files['dlt'][1], + config.regular.files['dlt'][1], + ]) + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let gotten: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 200 || gotten) { return; } - stream - .observe( - new Factory.Concat() - .type(Factory.FileType.Binary) - .asDlt({ - fibex_file_paths: [], - filter_config: undefined, - with_storage_header: true, - tz: undefined, - }) - .files([ - config.regular.files['dlt'][1], - config.regular.files['dlt'][1], - ]) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let gotten: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 200 || gotten) { - return; - } - gotten = true; - stream - .grab(50, 100) - .then((grabbed) => { - expect(grabbed[54].source_id).toEqual(0); - expect(grabbed[55].source_id).toEqual(1); - const output = path.resolve(os.tmpdir(), `${v4()}.logs`); - stream - .exportRaw(output, [{ from: 50, to: 149 }]) - .then(() => { - session - .destroy() - .then(() => { - Session.create() - .then((session: Session) => { - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); + gotten = true; + comps.stream + .grab(50, 100) + .then((grabbed) => { + expect(grabbed[54].source_id).toEqual(0); + expect(grabbed[55].source_id).toEqual(1); + const output = path.resolve(os.tmpdir(), `${v4()}.logs`); + comps.stream + .exportRaw(output, [{ from: 50, to: 149 }]) + .then(() => { + comps.session + .destroy() + .then(async () => { + const { session, stream, search, events } = + await runners.initializeSession( + config.regular.list[7], + ); + if (stream instanceof Error) { + finish(session, done, stream); + return; + } + stream + .observe( + new Factory.File() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .file(output) + .get() + .sterilized(), + ) + .catch(finish.bind(null, session, done)); + if (events instanceof Error) { + finish(session, done, events); + return; + } + gotten = false; + events.StreamUpdated.subscribe( + (rows: number) => { + if (rows < 90 || gotten) { return; } + gotten = true; stream - .observe( - new Factory.File() - .type( - Factory.FileType.Binary, - ) - .asDlt({ - fibex_file_paths: [], - filter_config: - undefined, - with_storage_header: - true, - tz: - undefined, - }) - .file(output) - .get() - .sterilized(), - ) - .catch( - finish.bind( - null, - session, - done, - ), - ); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - gotten = false; - events.StreamUpdated.subscribe( - (rows: number) => { - if (rows < 90 || gotten) { - return; - } - gotten = true; - stream - .grab(0, 100) - .then((rows) => { - expect( - rows.length, - ).toEqual( - grabbed.length, + .grab(0, 100) + .then((rows) => { + expect(rows.length).toEqual( + grabbed.length, + ); + for ( + let i = 0; + i < rows.length; + i += 1 + ) { + expect( + rows[i].content, + ).toEqual( + grabbed[i].content, + ); + if ( + rows[i].content !== + grabbed[i].content + ) { + console.log( + `Rows are dismatch. Stream position ${grabbed[i].position}.`, ); - for ( - let i = 0; - i < rows.length; - i += 1 - ) { - expect( - rows[i].content, - ).toEqual( - grabbed[i] - .content, - ); - if ( - rows[i] - .content !== - grabbed[i] - .content - ) { - console.log( - `Rows are dismatch. Stream position ${grabbed[i].position}.`, - ); - return finish( - session, - done, - ); - } - } - finish(session, done); - }) - .catch((err: Error) => { - finish( - undefined, + return finish( + session, done, - new Error( - `Fail to grab due error: ${ - err instanceof - Error - ? err.message - : err - }`, - ), ); - }); - }, - ); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to destroy session due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), + } + } + finish(session, done); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to grab due error: ${ + err instanceof Error + ? err.message + : err + }`, + ), + ); + }); + }, ); - }); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to export data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to grab due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to destroy session due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to export data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to grab due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); }); }); }); it(config.regular.list[8], function () { - return runner(config.regular, 8, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); + return runners.withSession(config.regular, 8, async (logger, done, comps) => { + comps.stream + .observe( + new Factory.Concat() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .files([ + config.regular.files['dlt'][1], + config.regular.files['dlt'][1], + ]) + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let gotten: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows < 200 || gotten) { return; } - stream - .observe( - new Factory.Concat() - .type(Factory.FileType.Binary) - .asDlt({ - fibex_file_paths: [], - filter_config: undefined, - with_storage_header: true, - tz: undefined, - }) - .files([ - config.regular.files['dlt'][1], - config.regular.files['dlt'][1], - ]) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let gotten: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows < 200 || gotten) { - return; - } - const ranges = [ - { - from: 50, - to: 60, - }, - { - from: 150, - to: 160, - }, - ]; - gotten = true; - Promise.all(ranges.map((r) => stream.grab(r.from, r.to - r.from))) - .then((results) => { - let grabbed: IGrabbedElement[] = []; - results.forEach((g) => (grabbed = grabbed.concat(g))); - grabbed.sort((a, b) => (a.position > b.position ? 1 : -1)); - const output = path.resolve(os.tmpdir(), `${v4()}.logs`); - stream - .exportRaw( - output, - ranges.map((r) => { - return { from: r.from, to: r.to - 1 }; - }), - ) - .then(() => { - session - .destroy() - .then(() => { - Session.create() - .then((session: Session) => { - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); + const ranges = [ + { + from: 50, + to: 60, + }, + { + from: 150, + to: 160, + }, + ]; + gotten = true; + Promise.all(ranges.map((r) => comps.stream.grab(r.from, r.to - r.from))) + .then((results) => { + let grabbed: IGrabbedElement[] = []; + results.forEach((g) => (grabbed = grabbed.concat(g))); + grabbed.sort((a, b) => (a.position > b.position ? 1 : -1)); + const output = path.resolve(os.tmpdir(), `${v4()}.logs`); + comps.stream + .exportRaw( + output, + ranges.map((r) => { + return { from: r.from, to: r.to - 1 }; + }), + ) + .then(() => { + comps.session + .destroy() + .then(async () => { + const { session, stream, search, events } = + await runners.initializeSession( + config.regular.list[8], + ); + stream + .observe( + new Factory.File() + .type(Factory.FileType.Binary) + .asDlt({ + fibex_file_paths: [], + filter_config: undefined, + with_storage_header: true, + tz: undefined, + }) + .file(output) + .get() + .sterilized(), + ) + .catch(finish.bind(null, session, done)); + gotten = false; + events.StreamUpdated.subscribe( + (rows: number) => { + if (rows < 20 || gotten) { return; } + gotten = true; stream - .observe( - new Factory.File() - .type( - Factory.FileType.Binary, - ) - .asDlt({ - fibex_file_paths: [], - filter_config: - undefined, - with_storage_header: - true, - tz: - undefined, - }) - .file(output) - .get() - .sterilized(), - ) - .catch( - finish.bind( - null, - session, - done, - ), - ); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - gotten = false; - events.StreamUpdated.subscribe( - (rows: number) => { - if (rows < 20 || gotten) { - return; - } - gotten = true; - stream - .grab(0, 20) - .then((rows) => { - expect( - rows.length, - ).toEqual( - grabbed.length, + .grab(0, 20) + .then((rows) => { + expect(rows.length).toEqual( + grabbed.length, + ); + for ( + let i = 0; + i < rows.length; + i += 1 + ) { + expect( + rows[i].content, + ).toEqual( + grabbed[i].content, + ); + if ( + rows[i].content !== + grabbed[i].content + ) { + console.log( + `Rows are dismatch. Stream position ${grabbed[i].position}.`, ); - for ( - let i = 0; - i < rows.length; - i += 1 - ) { - expect( - rows[i].content, - ).toEqual( - grabbed[i] - .content, - ); - if ( - rows[i] - .content !== - grabbed[i] - .content - ) { - console.log( - `Rows are dismatch. Stream position ${grabbed[i].position}.`, - ); - return finish( - session, - done, - ); - } - } - finish(session, done); - }) - .catch((err: Error) => { - finish( - undefined, + return finish( + session, done, - new Error( - `Fail to grab due error: ${ - err instanceof - Error - ? err.message - : err - }`, - ), ); - }); - }, - ); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to destroy session due error: ${ - err instanceof Error - ? err.message - : err - }`, - ), + } + } + finish(session, done); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to grab due error: ${ + err instanceof Error + ? err.message + : err + }`, + ), + ); + }); + }, ); - }); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to export data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to grab due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to destroy session due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to export data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); + }) + .catch((err: Error) => { + finish( + undefined, + done, + new Error( + `Fail to grab due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); + }); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.extract.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.extract.spec.ts index 44d133293..c134bce84 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.extract.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.extract.spec.ts @@ -1,206 +1,148 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; -import { createSampleFile, finish, runner } from './common'; +import { Factory } from '../src/api/session'; +import { createSampleFile, finish } from './common'; import { readConfigurationFile } from './config'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.extract; describe('Extracting', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const search = session.getSearch(); - if (search instanceof Error) { - return finish(session, done, search); - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - let controlSum = 0; - const tmpobj = createSampleFile(5000, logger, (i: number) => { - const value = i % 100 === 0 || i <= 5 ? i : -1; - controlSum += value !== -1 ? value : 0; - return `[${i}]:: ${ - value !== -1 ? `some CPU=${value}% line data\n` : `some line data\n` - }`; - }); + return runners.withSession(config.regular, 1, async (logger, done, comps) => { + let controlSum = 0; + const tmpobj = createSampleFile(5000, logger, (i: number) => { + const value = i % 100 === 0 || i <= 5 ? i : -1; + controlSum += value !== -1 ? value : 0; + return `[${i}]:: ${ + value !== -1 ? `some CPU=${value}% line data\n` : `some line data\n` + }`; + }); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - const filter = 'cpu=(\\d{1,})'; - search - .extract([ - { - filter, - flags: { reg: true, word: false, cases: false }, - }, - ]) - .then((results) => { - expect(results.length).toEqual(55); - for (let pos = 0; pos <= 5; pos += 1) { - expect(results[pos].position).toEqual(pos); - } - for (let pos = 1; pos <= 49; pos += 1) { - expect(results[pos + 5].position).toEqual(pos * 100); - } - let calculated = 0; - results.forEach((res) => { - expect(res.values.length).toEqual(1); - expect(res.values[0].filter.filter).toEqual(filter); - calculated += parseInt(res.values[0].values[0], 10); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + const filter = 'cpu=(\\d{1,})'; + comps.search + .extract([ + { + filter, + flags: { reg: true, word: false, cases: false }, + }, + ]) + .then((results) => { + expect(results.length).toEqual(55); + for (let pos = 0; pos <= 5; pos += 1) { + expect(results[pos].position).toEqual(pos); + } + for (let pos = 1; pos <= 49; pos += 1) { + expect(results[pos + 5].position).toEqual(pos * 100); + } + let calculated = 0; + results.forEach((res) => { + expect(res.values.length).toEqual(1); + expect(res.values[0].filter.filter).toEqual(filter); + calculated += parseInt(res.values[0].values[0], 10); + }); + expect(calculated).toEqual(controlSum); + comps.search + .len() + .then((len: number) => { + expect(len).toEqual(0); + finish(comps.session, done); + }) + .catch((err: Error) => { + finish(comps.session, done, err); }); - expect(calculated).toEqual(controlSum); - search - .len() - .then((len: number) => { - expect(len).toEqual(0); - finish(session, done); - }) - .catch((err: Error) => { - finish(session, done, err); - }); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const search = session.getSearch(); - if (search instanceof Error) { - return finish(session, done, search); - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - let controlSumA = 0; - let controlSumB = 0; - const tmpobj = createSampleFile(5000, logger, (i: number) => { - const a = i % 100 === 0 || i <= 5 ? i : -1; - controlSumA += a !== -1 ? a : 0; - const b = a === -1 && i % 20 === 0 ? i : -1; - controlSumB += b !== -1 ? b : 0; - return `[${i}]:: ${ - a !== -1 - ? `some CPU=${a}% line data\n` - : b === -1 - ? `some line data\n` - : `some TEMP=${b}C line data\n` - }`; - }); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - const filterA = 'cpu=(\\d{1,})'; - const filterB = 'temp=(\\d{1,})'; - search - .extract([ - { - filter: filterA, - flags: { reg: true, word: false, cases: false }, - }, - { - filter: filterB, - flags: { reg: true, word: false, cases: false }, - }, - ]) - .then((results) => { - let calculatedA = 0; - let calculatedB = 0; - results.forEach((res) => { - res.values.forEach((match) => { - expect(match.values.length).toEqual(1); - if (match.filter.filter === filterA) { - calculatedA += parseInt(match.values[0], 10); - } - if (match.filter.filter === filterB) { - calculatedB += parseInt(match.values[0], 10); - } - }); - }); - expect(calculatedA).toEqual(controlSumA); - expect(calculatedB).toEqual(controlSumB); - search - .len() - .then((len: number) => { - expect(len).toEqual(0); - finish(session, done); - }) - .catch((err: Error) => { - finish(session, done, err); - }); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); + return runners.withSession(config.regular, 2, async (logger, done, comps) => { + let controlSumA = 0; + let controlSumB = 0; + const tmpobj = createSampleFile(5000, logger, (i: number) => { + const a = i % 100 === 0 || i <= 5 ? i : -1; + controlSumA += a !== -1 ? a : 0; + const b = a === -1 && i % 20 === 0 ? i : -1; + controlSumB += b !== -1 ? b : 0; + return `[${i}]:: ${ + a !== -1 + ? `some CPU=${a}% line data\n` + : b === -1 + ? `some line data\n` + : `some TEMP=${b}C line data\n` + }`; }); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + const filterA = 'cpu=(\\d{1,})'; + const filterB = 'temp=(\\d{1,})'; + comps.search + .extract([ + { + filter: filterA, + flags: { reg: true, word: false, cases: false }, + }, + { + filter: filterB, + flags: { reg: true, word: false, cases: false }, + }, + ]) + .then((results) => { + let calculatedA = 0; + let calculatedB = 0; + results.forEach((res) => { + res.values.forEach((match) => { + expect(match.values.length).toEqual(1); + if (match.filter.filter === filterA) { + calculatedA += parseInt(match.values[0], 10); + } + if (match.filter.filter === filterB) { + calculatedB += parseInt(match.values[0], 10); + } + }); + }); + expect(calculatedA).toEqual(controlSumA); + expect(calculatedB).toEqual(controlSumB); + comps.search + .len() + .then((len: number) => { + expect(len).toEqual(0); + finish(comps.session, done); + }) + .catch((err: Error) => { + finish(comps.session, done, err); + }); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.indexes.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.indexes.spec.ts index ef3126f03..c02456913 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.indexes.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.indexes.spec.ts @@ -1,43 +1,23 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; -import { createSampleFile, finish, performanceReport, setMeasurement, runner } from './common'; +import { Factory } from '../src/api/session'; +import { createSampleFile, finish } from './common'; import { readConfigurationFile } from './config'; import { Nature, IndexingMode, NatureTypes } from 'platform/types/content'; -import * as os from 'os'; -import * as path from 'path'; +import * as runners from './runners'; const config = readConfigurationFile().get().tests.indexes; describe('Indexes', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { + return runners.withSession(config.regular, 1, async (logger, done, comps) => { (async () => { - try { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const search = session.getSearch(); - if (search instanceof Error) { - finish(session, done, search); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } let controlSum = 0; let countMatches = 0; const tmpobj = createSampleFile(50, logger, (i: number) => { @@ -46,32 +26,33 @@ describe('Indexes', function () { return `${i}: some line data: ${i % 10 == 0 ? `match A` : ''}\n`; }); let read: boolean = false; - stream + comps.stream .observe( new Factory.File() .asText() .type(Factory.FileType.Text) .file(tmpobj.name) - .get().sterilized(), + .get() + .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); const updates: number[] = []; - events.IndexedMapUpdated.subscribe((event) => { + comps.events.IndexedMapUpdated.subscribe((event) => { event.len > 0 && updates.push(event.len); }); - events.StreamUpdated.subscribe(async (rows: number) => { + comps.events.StreamUpdated.subscribe(async (rows: number) => { if (rows < 50 || read) { return; } read = true; try { - await search.search([ + await comps.search.search([ { filter: 'match A', flags: { reg: true, word: true, cases: false }, }, ]); - let items = await stream.grabIndexed(0, countMatches); + let items = await comps.stream.grabIndexed(0, countMatches); expect(items.length).toEqual(countMatches); expect( items @@ -92,10 +73,10 @@ describe('Indexes', function () { [30, [NatureTypes.Search]], [40, [NatureTypes.Search]], ]); - await stream.setIndexingMode(IndexingMode.Breadcrumbs); - let len = await stream.getIndexedLen(); + await comps.stream.setIndexingMode(IndexingMode.Breadcrumbs); + let len = await comps.stream.getIndexedLen(); expect(len).toEqual(30); - items = await stream.grabIndexed(0, len); + items = await comps.stream.grabIndexed(0, len); expect(items.length).toEqual(len); expect( items.map((i) => [i.position, new Nature(i.nature).getTypes()]), @@ -131,10 +112,10 @@ describe('Indexes', function () { [48, [NatureTypes.Breadcrumb]], [49, [NatureTypes.Breadcrumb]], ]); - await stream.expandBreadcrumbs(45, 2, false); - len = await stream.getIndexedLen(); + await comps.stream.expandBreadcrumbs(45, 2, false); + len = await comps.stream.getIndexedLen(); expect(len).toEqual(32); - items = await stream.grabIndexed(0, len); + items = await comps.stream.grabIndexed(0, len); expect(items.length).toEqual(len); expect( items.map((i) => [i.position, new Nature(i.nature).getTypes()]), @@ -172,10 +153,10 @@ describe('Indexes', function () { [48, [NatureTypes.Breadcrumb]], [49, [NatureTypes.Breadcrumb]], ]); - await stream.expandBreadcrumbs(44, 2, true); - len = await stream.getIndexedLen(); + await comps.stream.expandBreadcrumbs(44, 2, true); + len = await comps.stream.getIndexedLen(); expect(len).toEqual(34); - items = await stream.grabIndexed(0, len); + items = await comps.stream.grabIndexed(0, len); expect(items.length).toEqual(len); expect( items.map((i) => [i.position, new Nature(i.nature).getTypes()]), @@ -215,10 +196,10 @@ describe('Indexes', function () { [48, [NatureTypes.Breadcrumb]], [49, [NatureTypes.Breadcrumb]], ]); - await stream.expandBreadcrumbs(45, 1, true); - len = await stream.getIndexedLen(); + await comps.stream.expandBreadcrumbs(45, 1, true); + len = await comps.stream.getIndexedLen(); expect(len).toEqual(34); - items = await stream.grabIndexed(0, len); + items = await comps.stream.grabIndexed(0, len); expect(items.length).toEqual(len); expect( items.map((i) => [i.position, new Nature(i.nature).getTypes()]), @@ -258,7 +239,7 @@ describe('Indexes', function () { [48, [NatureTypes.Breadcrumb]], [49, [NatureTypes.Breadcrumb]], ]); - finish(session, done); + finish(comps.session, done); } catch (err) { finish( undefined, @@ -272,9 +253,6 @@ describe('Indexes', function () { } }); return Promise.resolve(); - } catch (err) { - return Promise.reject(err instanceof Error ? err : new Error(`${err}`)); - } })().catch((err: Error) => { finish( undefined, diff --git a/application/apps/rustcore/ts-bindings/spec/session.jobs.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.jobs.spec.ts index 15e98d96a..c2dcf2a0c 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.jobs.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.jobs.spec.ts @@ -1,5 +1,4 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// @@ -8,15 +7,16 @@ import { initLogger } from './logger'; initLogger(); import { Jobs, Tracker } from '../src/index'; import { readConfigurationFile } from './config'; -import { finish, runner } from './common'; +import { finish } from './common'; +import * as runners from './runners'; import * as os from 'os'; const config = readConfigurationFile().get().tests.jobs; describe('Jobs', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { + return runners.unbound(config.regular, 1, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; const tracker = collector(await Tracker.create()) as Tracker; const operations: Map = new Map(); @@ -61,7 +61,7 @@ describe('Jobs', function () { }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { + return runners.unbound(config.regular, 2, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; // Run 2 jobs with same sequence. One of jobs should be failed, because of sequence Promise.allSettled([ @@ -91,7 +91,7 @@ describe('Jobs', function () { }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { + return runners.unbound(config.regular, 3, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; const path = os.homedir(); jobs.listContent({ @@ -131,7 +131,7 @@ describe('Jobs', function () { }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { + return runners.unbound(config.regular, 4, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; const profiles = await jobs.getShellProfiles(); expect(profiles.length > 0).toBe(true); @@ -140,7 +140,7 @@ describe('Jobs', function () { }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { + return runners.unbound(config.regular, 5, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; const envvars = await jobs.getContextEnvvars(); expect(envvars.size > 0).toBe(true); @@ -150,7 +150,7 @@ describe('Jobs', function () { }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { + return runners.unbound(config.regular, 6, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; const path = config.regular.files['someip-pcapng']; // test single source @@ -241,7 +241,7 @@ describe('Jobs', function () { }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { + return runners.unbound(config.regular, 7, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; const path = config.regular.files['sample-txt']; jobs.isFileBinary({ @@ -254,12 +254,12 @@ describe('Jobs', function () { }) .catch((err: Error) => { finish(jobs, done, err); - }) - }) + }); + }); }); it(config.regular.list[8], function () { - return runner(config.regular, 8, async (logger, done, collector) => { + return runners.unbound(config.regular, 8, async (logger, done, collector) => { const jobs = collector(await Jobs.create()) as Jobs; // Run sleeping, but do not wait for it jobs.sleep(6000).then(() => { diff --git a/application/apps/rustcore/ts-bindings/spec/session.map.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.map.spec.ts index cd5c5bb8b..504e3b564 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.map.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.map.spec.ts @@ -1,20 +1,22 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { ISearchMap } from 'platform/types/filter'; -import { finish, createSampleFile, runner } from './common'; +import { finish, createSampleFile } from './common'; import { readConfigurationFile } from './config'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.map; describe('Map', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { + return runners.withSession(config.regular, 1, async (logger, done, comps) => { const index: number = 1; if ( config.regular.spec === undefined || @@ -28,81 +30,57 @@ describe('Map', function () { ); } const spec = config.regular.spec.map[index]; - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[index]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - spec.filesize, - logger, - (i: number) => - `[${i}]:: some ${i % 100 === 0 || i < 5 ? 'match' : ''} line data }\n`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'match', - flags: { reg: false, word: false, cases: false }, - }, - ]) - .then((_) => { - search - .getMap(spec.datasetLength) - .then((map: ISearchMap) => { - expect(map.length).toEqual(spec.filesize); - map.forEach((values: number[][], line: number) => { - if (line % 100 === 0 || line < 5) { - expect(values.length).toEqual(1); - values.forEach((matches: number[]) => { - expect(matches[0]).toEqual(0); - expect(matches[1]).toEqual(1); - }); - } else { - expect(values.length).toEqual(0); - } - }); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + const tmpobj = createSampleFile( + spec.filesize, + logger, + (i: number) => + `[${i}]:: some ${i % 100 === 0 || i < 5 ? 'match' : ''} line data }\n`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: 'match', + flags: { reg: false, word: false, cases: false }, + }, + ]) + .then((_) => { + comps.search + .getMap(spec.datasetLength) + .then((map: ISearchMap) => { + expect(map.length).toEqual(spec.filesize); + map.forEach((values: number[][], line: number) => { + if (line % 100 === 0 || line < 5) { + expect(values.length).toEqual(1); + values.forEach((matches: number[]) => { + expect(matches[0]).toEqual(0); + expect(matches[1]).toEqual(1); + }); + } else { + expect(values.length).toEqual(0); + } + }); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { + return runners.withSession(config.regular, 2, async (logger, done, comps) => { const index: number = 2; if ( config.regular.spec === undefined || @@ -116,83 +94,57 @@ describe('Map', function () { ); } const spec = config.regular.spec.map[index]; - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[index]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - spec.filesize, - logger, - (i: number) => - `[${i}]:: some ${i % 100 === 0 || i < 5 ? 'match' : ''} line data }\n`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'match', - flags: { reg: false, word: false, cases: false }, - }, - ]) - .then((_) => { - search - .getMap(spec.datasetLength) - .then((map: ISearchMap) => { - expect(map.length).toEqual(spec.datasetLength); - map.forEach( - (lineData: number[][], lineNumber: number) => { - if (lineNumber % 100 === 0 || lineNumber < 5) { - expect(lineData.length).toEqual(1); - lineData.forEach((matches: number[]) => { - expect(matches[0]).toEqual(0); - expect(matches[1]).toEqual(1); - }); - } else { - expect(lineData.length).toEqual(0); - } - }, - ); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + const tmpobj = createSampleFile( + spec.filesize, + logger, + (i: number) => + `[${i}]:: some ${i % 100 === 0 || i < 5 ? 'match' : ''} line data }\n`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: 'match', + flags: { reg: false, word: false, cases: false }, + }, + ]) + .then((_) => { + comps.search + .getMap(spec.datasetLength) + .then((map: ISearchMap) => { + expect(map.length).toEqual(spec.datasetLength); + map.forEach((lineData: number[][], lineNumber: number) => { + if (lineNumber % 100 === 0 || lineNumber < 5) { + expect(lineData.length).toEqual(1); + lineData.forEach((matches: number[]) => { + expect(matches[0]).toEqual(0); + expect(matches[1]).toEqual(1); + }); + } else { + expect(lineData.length).toEqual(0); + } + }); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { + return runners.withSession(config.regular, 3, async (logger, done, comps) => { const index: number = 3; if ( config.regular.spec === undefined || @@ -206,136 +158,109 @@ describe('Map', function () { ); } const spec = config.regular.spec.map[index]; - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[index]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - spec.filesize, - logger, - (i: number) => - `[${i}]:: some ${i % 100 === 0 ? 'match' : ''} line data ${ - i % 33 === 0 || i % 55 === 0 ? 'not' : '' - }\n`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'match', - flags: { reg: false, word: false, cases: true }, - }, - { - filter: 'not', - flags: { reg: false, word: false, cases: false }, - }, - { - filter: 'line', - flags: { reg: false, word: false, cases: true }, - }, - ]) - .then((_) => { - search - .getMap(spec.datasetLength) - .then((map: ISearchMap) => { - expect(map.length).toEqual(spec.datasetLength); - map.forEach( - (lineData: number[][], lineNumber: number) => { + const tmpobj = createSampleFile( + spec.filesize, + logger, + (i: number) => + `[${i}]:: some ${i % 100 === 0 ? 'match' : ''} line data ${ + i % 33 === 0 || i % 55 === 0 ? 'not' : '' + }\n`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: 'match', + flags: { reg: false, word: false, cases: true }, + }, + { + filter: 'not', + flags: { reg: false, word: false, cases: false }, + }, + { + filter: 'line', + flags: { reg: false, word: false, cases: true }, + }, + ]) + .then((_) => { + comps.search + .getMap(spec.datasetLength) + .then((map: ISearchMap) => { + expect(map.length).toEqual(spec.datasetLength); + map.forEach((lineData: number[][], lineNumber: number) => { + if ( + lineNumber % 100 === 0 && + (lineNumber % 33 === 0 || lineNumber % 55 === 0) + ) { + expect(lineData.length).toEqual(3); + lineData.forEach((matches: number[]) => { if ( - lineNumber % 100 === 0 && - (lineNumber % 33 === 0 || - lineNumber % 55 === 0) - ) { - expect(lineData.length).toEqual(3); - lineData.forEach((matches: number[]) => { - if ( - matches[0] === 0 || - matches[0] === 1 || - matches[0] === 2 - ) { - expect(matches[1]).toEqual(1); - } else { - expect(matches[0]).toBeUndefined(); - expect(matches[1]).toBeUndefined(); - } - }); - } else if (lineNumber % 100 === 0) { - expect(lineData.length).toEqual(2); - lineData.forEach((matches: number[]) => { - if (matches[0] === 0) { - expect(matches[1]).toEqual(1); - } else if (matches[0] === 2) { - expect(matches[1]).toEqual(1); - } else { - expect(matches[0]).toBeUndefined(); - expect(matches[1]).toBeUndefined(); - } - }); - } else if ( - lineNumber % 33 === 0 || - lineNumber % 55 === 0 + matches[0] === 0 || + matches[0] === 1 || + matches[0] === 2 ) { - expect(lineData.length).toEqual(2); - lineData.forEach((matches: number[]) => { - if (matches[0] === 1) { - expect(matches[1]).toEqual(1); - } else if (matches[0] === 2) { - expect(matches[1]).toEqual(1); - } else { - expect(matches[0]).toBeUndefined(); - expect(matches[1]).toBeUndefined(); - } - }); + expect(matches[1]).toEqual(1); + } else { + expect(matches[0]).toBeUndefined(); + expect(matches[1]).toBeUndefined(); + } + }); + } else if (lineNumber % 100 === 0) { + expect(lineData.length).toEqual(2); + lineData.forEach((matches: number[]) => { + if (matches[0] === 0) { + expect(matches[1]).toEqual(1); + } else if (matches[0] === 2) { + expect(matches[1]).toEqual(1); } else { - expect(lineData.length).toEqual(1); - lineData.forEach((matches: number[]) => { - expect(matches[0]).toEqual(2); - expect(matches[1]).toEqual(1); - }); + expect(matches[0]).toBeUndefined(); + expect(matches[1]).toBeUndefined(); } - }, - ); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + }); + } else if ( + lineNumber % 33 === 0 || + lineNumber % 55 === 0 + ) { + expect(lineData.length).toEqual(2); + lineData.forEach((matches: number[]) => { + if (matches[0] === 1) { + expect(matches[1]).toEqual(1); + } else if (matches[0] === 2) { + expect(matches[1]).toEqual(1); + } else { + expect(matches[0]).toBeUndefined(); + expect(matches[1]).toBeUndefined(); + } + }); + } else { + expect(lineData.length).toEqual(1); + lineData.forEach((matches: number[]) => { + expect(matches[0]).toEqual(2); + expect(matches[1]).toEqual(1); + }); + } + }); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { + return runners.withSession(config.regular, 4, async (logger, done, comps) => { const index: number = 4; if ( config.regular.spec === undefined || @@ -349,84 +274,59 @@ describe('Map', function () { ); } const spec = config.regular.spec.map[index]; - Session.create() - .then((session: Session) => { - session.debug(true, config.regular.list[index]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - spec.filesize, - logger, - (i: number) => - `[${i}]:: Run command ${ - i % 100 === 0 || i < 5 ? 'echo "haha">>file.txt' : '' - }\n`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'file.txt', - flags: { reg: false, word: false, cases: false }, - }, - ]) - .then((_) => { - search - .getMap(spec.datasetLength) - .then((map: ISearchMap) => { - expect(map.length).toEqual(spec.datasetLength); - map.forEach( - (lineData: number[][], lineNumber: number) => { - if (lineNumber % 100 === 0 || lineNumber < 5) { - expect(lineData.length).toEqual(1); - lineData.forEach((matches: number[]) => { - expect(matches[0]).toEqual(0); - expect(matches[1]).toEqual(1); - }); - } else { - expect(lineData.length).toEqual(0); - } - }, - ); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + const tmpobj = createSampleFile( + spec.filesize, + logger, + (i: number) => + `[${i}]:: Run command ${ + i % 100 === 0 || i < 5 ? 'echo "haha">>file.txt' : '' + }\n`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: 'file.txt', + flags: { reg: false, word: false, cases: false }, + }, + ]) + .then((_) => { + comps.search + .getMap(spec.datasetLength) + .then((map: ISearchMap) => { + expect(map.length).toEqual(spec.datasetLength); + map.forEach((lineData: number[][], lineNumber: number) => { + if (lineNumber % 100 === 0 || lineNumber < 5) { + expect(lineData.length).toEqual(1); + lineData.forEach((matches: number[]) => { + expect(matches[0]).toEqual(0); + expect(matches[1]).toEqual(1); + }); + } else { + expect(lineData.length).toEqual(0); + } + }); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { + return runners.withSession(config.regular, 5, async (logger, done, comps) => { const index: number = 5; if ( config.regular.spec === undefined || @@ -440,85 +340,57 @@ describe('Map', function () { ); } const spec = config.regular.spec.map[index]; - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[index]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - spec.filesize, - logger, - (i: number) => - `[${i}]:: Random text ${ - i % 100 === 0 || i < 5 ? '1:1' : '' - } as expected\n`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: '1:1', - flags: { reg: false, word: false, cases: false }, - }, - ]) - .then((_) => { - search - .getMap(spec.datasetLength) - .then((map: ISearchMap) => { - expect(map.length).toEqual(spec.datasetLength); - map.forEach( - (lineData: number[][], lineNumber: number) => { - if (lineNumber % 100 === 0 || lineNumber < 5) { - expect(lineData.length).toEqual(1); - lineData.forEach((matches: number[]) => { - expect(matches[0]).toEqual(0); - expect(matches[1]).toEqual(1); - }); - } else { - expect(lineData.length).toEqual(0); - } - }, - ); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + const tmpobj = createSampleFile( + spec.filesize, + logger, + (i: number) => + `[${i}]:: Random text ${i % 100 === 0 || i < 5 ? '1:1' : ''} as expected\n`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: '1:1', + flags: { reg: false, word: false, cases: false }, + }, + ]) + .then((_) => { + comps.search + .getMap(spec.datasetLength) + .then((map: ISearchMap) => { + expect(map.length).toEqual(spec.datasetLength); + map.forEach((lineData: number[][], lineNumber: number) => { + if (lineNumber % 100 === 0 || lineNumber < 5) { + expect(lineData.length).toEqual(1); + lineData.forEach((matches: number[]) => { + expect(matches[0]).toEqual(0); + expect(matches[1]).toEqual(1); + }); + } else { + expect(lineData.length).toEqual(0); + } + }); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { + return runners.withSession(config.regular, 6, async (logger, done, comps) => { const index: number = 6; if ( config.regular.spec === undefined || @@ -532,85 +404,59 @@ describe('Map', function () { ); } const spec = config.regular.spec.map[index]; - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[index]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - spec.filesize, - logger, - (i: number) => - `[${i}]:: Timestamp is ${ - i % 15 === 0 || i < 7 ? '00:00.0:1' + i : 'unknown' - } right now.\n`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: '0.0:1', - flags: { reg: false, word: false, cases: false }, - }, - ]) - .then((_) => { - search - .getMap(spec.datasetLength) - .then((map: ISearchMap) => { - expect(map.length).toEqual(spec.datasetLength); - map.forEach( - (lineData: number[][], lineNumber: number) => { - if (lineNumber % 15 === 0 || lineNumber < 7) { - expect(lineData.length).toEqual(1); - lineData.forEach((matches: number[]) => { - expect(matches[0]).toEqual(0); - expect(matches[1]).toEqual(1); - }); - } else { - expect(lineData.length).toEqual(0); - } - }, - ); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + const tmpobj = createSampleFile( + spec.filesize, + logger, + (i: number) => + `[${i}]:: Timestamp is ${ + i % 15 === 0 || i < 7 ? '00:00.0:1' + i : 'unknown' + } right now.\n`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: '0.0:1', + flags: { reg: false, word: false, cases: false }, + }, + ]) + .then((_) => { + comps.search + .getMap(spec.datasetLength) + .then((map: ISearchMap) => { + expect(map.length).toEqual(spec.datasetLength); + map.forEach((lineData: number[][], lineNumber: number) => { + if (lineNumber % 15 === 0 || lineNumber < 7) { + expect(lineData.length).toEqual(1); + lineData.forEach((matches: number[]) => { + expect(matches[0]).toEqual(0); + expect(matches[1]).toEqual(1); + }); + } else { + expect(lineData.length).toEqual(0); + } + }); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { + return runners.withSession(config.regular, 7, async (logger, done, comps) => { const index: number = 7; if ( config.regular.spec === undefined || @@ -624,80 +470,54 @@ describe('Map', function () { ); } const spec = config.regular.spec.map[index]; - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[index]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } - const tmpobj = createSampleFile( - spec.filesize, - logger, - (i: number) => - `[${i}]:: Timestamp in a longsword(${ - i % 3 === 0 || i > 700 ? '0.0:1' + i : 'unknown' - })\n`, - ); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .search([ - { - filter: 'word(0.0:1', - flags: { reg: false, word: false, cases: false }, - }, - ]) - .then((_) => { - search - .getMap(spec.datasetLength) - .then((map: ISearchMap) => { - expect(map.length).toEqual(spec.datasetLength); - map.forEach( - (lineData: number[][], lineNumber: number) => { - if (lineNumber % 3 === 0 || lineNumber > 700) { - expect(lineData.length).toEqual(1); - lineData.forEach((matches: number[]) => { - expect(matches[0]).toEqual(0); - expect(matches[1]).toEqual(1); - }); - } else { - expect(lineData.length).toEqual(0); - } - }, - ); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + const tmpobj = createSampleFile( + spec.filesize, + logger, + (i: number) => + `[${i}]:: Timestamp in a longsword(${ + i % 3 === 0 || i > 700 ? '0.0:1' + i : 'unknown' + })\n`, + ); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search + .search([ + { + filter: 'word(0.0:1', + flags: { reg: false, word: false, cases: false }, + }, + ]) + .then((_) => { + comps.search + .getMap(spec.datasetLength) + .then((map: ISearchMap) => { + expect(map.length).toEqual(spec.datasetLength); + map.forEach((lineData: number[][], lineNumber: number) => { + if (lineNumber % 3 === 0 || lineNumber > 700) { + expect(lineData.length).toEqual(1); + lineData.forEach((matches: number[]) => { + expect(matches[0]).toEqual(0); + expect(matches[1]).toEqual(1); + }); + } else { + expect(lineData.length).toEqual(0); + } + }); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.observe.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.observe.spec.ts index 917e6da11..0ea62fa48 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.observe.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.observe.spec.ts @@ -1,45 +1,31 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IGrabbedElement } from 'platform/types/content'; import { IAttachmentsUpdatedUpdated } from '../src/api/session.provider'; import { IAttachment } from 'platform/types/content'; -import { createSampleFile, finish, performanceReport, setMeasurement, runner } from './common'; +import { createSampleFile, finish } from './common'; import { readConfigurationFile } from './config'; + import * as fs from 'fs'; -import * as os from 'os'; -import * as path from 'path'; +import * as runners from './runners'; const config = readConfigurationFile().get().tests.observe; describe('Observe', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 1, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, (i: number) => `some line data: ${i}\n`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -48,14 +34,14 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let grabbing: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { if (rows === 0 || grabbing) { return; } grabbing = true; - stream + comps.stream .grab(500, 7) .then((result: IGrabbedElement[]) => { logger.debug('result of grab was: ' + JSON.stringify(result)); @@ -68,11 +54,11 @@ describe('Observe', function () { 'some line data: 505', 'some line data: 506', ]); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -82,39 +68,12 @@ describe('Observe', function () { ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - - stream + return runners.withSession(config.regular, 2, async (logger, done, comps) => { + comps.stream .observe( new Factory.File() .type(Factory.FileType.PcapNG) @@ -128,35 +87,35 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let grabbing: boolean = false; let received: number = 0; const timeout = setTimeout(() => { finish( - session, + comps.session, done, new Error( `Failed because timeout. Waited for at least 100 rows. Has been gotten: ${received}`, ), ); }, 20000); - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { received = rows; if (rows < 100 || grabbing) { return; } clearTimeout(timeout); grabbing = true; - stream + comps.stream .grab(1, 10) .then((result: IGrabbedElement[]) => { expect(result.length).toEqual(10); logger.debug('result of grab was: ' + JSON.stringify(result)); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -166,39 +125,12 @@ describe('Observe', function () { ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - - stream + return runners.withSession(config.regular, 3, async (logger, done, comps) => { + comps.stream .observe( new Factory.File() .type(Factory.FileType.Binary) @@ -212,35 +144,35 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let grabbing: boolean = false; let received: number = 0; const timeout = setTimeout(() => { finish( - session, + comps.session, done, new Error( `Failed because timeout. Waited for at least 100 rows. Has been gotten: ${received}`, ), ); }, 20000); - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { received = rows; if (rows < 100 || grabbing) { return; } clearTimeout(timeout); grabbing = true; - stream + comps.stream .grab(1, 10) .then((result: IGrabbedElement[]) => { expect(result.length).toEqual(10); logger.debug('result of grab was: ' + JSON.stringify(result)); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -250,38 +182,12 @@ describe('Observe', function () { ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - stream + return runners.withSession(config.regular, 4, async (logger, done, comps) => { + comps.stream .observe( new Factory.File() .type(Factory.FileType.Binary) @@ -295,18 +201,18 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let updates: IAttachmentsUpdatedUpdated[] = []; const timeout = setTimeout(() => { finish( - session, + comps.session, done, new Error( `Failed because timeout. Waited for at least 3 attachments. Has been gotten: ${updates.length}`, ), ); }, 20000); - events.AttachmentsUpdated.subscribe((update: IAttachmentsUpdatedUpdated) => { + comps.events.AttachmentsUpdated.subscribe((update: IAttachmentsUpdatedUpdated) => { updates.push(update); if (updates.length >= 3) { clearTimeout(timeout); @@ -346,41 +252,15 @@ describe('Observe', function () { 'test333', ); } - finish(session, done); + finish(comps.session, done); } }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - stream + return runners.withSession(config.regular, 5, async (logger, done, comps) => { + comps.stream .observe( new Factory.File() .type(Factory.FileType.PcapNG) @@ -391,26 +271,26 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let grabbing: boolean = false; let received: number = 0; const timeout = setTimeout(() => { finish( - session, + comps.session, done, new Error( `Failed because timeout. Waited for at least 55 rows. Has been gotten: ${received}`, ), ); }, 20000); - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { received = rows; if (rows < 55 || grabbing) { return; } clearTimeout(timeout); grabbing = true; - stream + comps.stream .grab(0, 4) .then((result: IGrabbedElement[]) => { expect(result.length).toEqual(4); @@ -443,11 +323,11 @@ describe('Observe', function () { 'Bytes: [00, 00, 01, 88, 01, C3, C4, 1D]', ]); logger.debug('result of grab was: ' + JSON.stringify(result)); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -457,38 +337,12 @@ describe('Observe', function () { ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - stream + return runners.withSession(config.regular, 6, async (logger, done, comps) => { + comps.stream .observe( new Factory.File() .type(Factory.FileType.PcapNG) @@ -499,26 +353,26 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let grabbing: boolean = false; let received: number = 0; const timeout = setTimeout(() => { finish( - session, + comps.session, done, new Error( `Failed because timeout. Waited for at least 55 rows. Has been gotten: ${received}`, ), ); }, 20000); - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { received = rows; if (rows < 55 || grabbing) { return; } clearTimeout(timeout); grabbing = true; - stream + comps.stream .grab(0, 4) .then((result: IGrabbedElement[]) => { expect(result.length).toEqual(4); @@ -551,11 +405,11 @@ describe('Observe', function () { 'TestService::timeEvent {timestamp(INT64):1683656786973,}', ]); logger.debug('result of grab was: ' + JSON.stringify(result)); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -565,38 +419,12 @@ describe('Observe', function () { ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - stream + return runners.withSession(config.regular, 7, async (logger, done, comps) => { + comps.stream .observe( new Factory.File() .type(Factory.FileType.PcapLegacy) @@ -607,26 +435,26 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let grabbing: boolean = false; let received: number = 0; const timeout = setTimeout(() => { finish( - session, + comps.session, done, new Error( `Failed because timeout. Waited for at least 55 rows. Has been gotten: ${received}`, ), ); }, 20000); - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { received = rows; if (rows < 55 || grabbing) { return; } clearTimeout(timeout); grabbing = true; - stream + comps.stream .grab(0, 4) .then((result: IGrabbedElement[]) => { expect(result.length).toEqual(4); @@ -659,11 +487,11 @@ describe('Observe', function () { 'Bytes: [00, 00, 01, 88, 01, C3, C4, 1D]', ]); logger.debug('result of grab was: ' + JSON.stringify(result)); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -673,38 +501,12 @@ describe('Observe', function () { ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[8], function () { - return runner(config.regular, 8, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - stream + return runners.withSession(config.regular, 8, async (logger, done, comps) => { + comps.stream .observe( new Factory.File() .type(Factory.FileType.PcapLegacy) @@ -715,26 +517,26 @@ describe('Observe', function () { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let grabbing: boolean = false; let received: number = 0; const timeout = setTimeout(() => { finish( - session, + comps.session, done, new Error( `Failed because timeout. Waited for at least 55 rows. Has been gotten: ${received}`, ), ); }, 20000); - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { received = rows; if (rows < 55 || grabbing) { return; } clearTimeout(timeout); grabbing = true; - stream + comps.stream .grab(0, 4) .then((result: IGrabbedElement[]) => { expect(result.length).toEqual(4); @@ -767,11 +569,11 @@ describe('Observe', function () { 'TestService::timeEvent {timestamp(INT64):1683656786973,}', ]); logger.debug('result of grab was: ' + JSON.stringify(result)); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -781,18 +583,6 @@ describe('Observe', function () { ); }); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.observing.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.observing.spec.ts index 9af09b9b0..628c807f6 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.observing.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.observing.spec.ts @@ -1,11 +1,16 @@ +// We need to provide path to TypeScript types definitions +/// +/// + import { initLogger } from './logger'; initLogger(); -import { finish, runner } from './common'; +import { finish } from './common'; import { readConfigurationFile } from './config'; import { Observer } from 'platform/env/observer'; import * as $ from 'platform/types/observe'; import * as Factory from 'platform/types/observe/factory'; +import * as runners from './runners'; const config = readConfigurationFile().get().tests.observing; @@ -70,7 +75,7 @@ function checkParser(observe: $.Observe, flags: { text: boolean; dlt: boolean; s describe('Platform: observing', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { + return runners.noSession(config.regular, 1, async (logger, done) => { const entity = new Observer({ a: 1, b: 2, @@ -131,7 +136,7 @@ describe('Platform: observing', function () { }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { + return runners.noSession(config.regular, 2, async (logger, done) => { const observe = new Factory.Stream().asDlt().tcp().get(); checkStream(observe, { udp: false, tcp: true, serial: false, process: false }); const stream = observe.origin.as<$.Origin.Stream.Configuration>( @@ -239,7 +244,7 @@ describe('Platform: observing', function () { }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { + return runners.noSession(config.regular, 3, async (logger, done) => { const observe = new Factory.File().type($.Types.File.FileType.Text).asText().get(); checkContext(observe, { file: true, concat: false, stream: false }); let file = observe.origin.as<$.Origin.File.Configuration>( @@ -265,7 +270,7 @@ describe('Platform: observing', function () { }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { + return runners.noSession(config.regular, 4, async (logger, done) => { const observe = new Factory.File().type($.Types.File.FileType.Text).asText().get(); checkParser(observe, { text: true, dlt: false, someip: false }); let file = observe.origin.as<$.Origin.File.Configuration>( @@ -294,7 +299,7 @@ describe('Platform: observing', function () { }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { + return runners.noSession(config.regular, 5, async (logger, done) => { const observe = new Factory.Stream().asDlt().tcp().get(); checkStream(observe, { udp: false, tcp: true, serial: false, process: false }); observe.subscribe(() => { @@ -419,7 +424,7 @@ describe('Platform: observing', function () { }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { + return runners.noSession(config.regular, 6, async (logger, done) => { const observe = new Factory.Stream().asText().serial().get(); checkContext(observe, { file: false, concat: false, stream: true }); const serial = observe.origin.as<$.Origin.Stream.Stream.Serial.Configuration>( diff --git a/application/apps/rustcore/ts-bindings/spec/session.promises.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.promises.spec.ts index 9767050af..d86d7ee51 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.promises.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.promises.spec.ts @@ -1,19 +1,21 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { finish, runner } from './common'; +import { finish } from './common'; import { readConfigurationFile } from './config'; import { CancelablePromise } from 'platform/env/promise'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.promises; describe('Promises', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { + return runners.noSession(config.regular, 1, async (logger, done) => { let resolved = 0; let rejected = 0; Promise.allSettled([ @@ -46,7 +48,7 @@ describe('Promises', function () { }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { + return runners.noSession(config.regular, 2, async (logger, done) => { new CancelablePromise((_resolve, reject) => { setTimeout(() => { reject(new Error(`Dummy error`)); @@ -70,7 +72,7 @@ describe('Promises', function () { }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { + return runners.noSession(config.regular, 3, async (logger, done) => { let delegated = false; new CancelablePromise((_resolve, reject, cancel, setCancelDelegation) => { setCancelDelegation(() => { @@ -101,7 +103,7 @@ describe('Promises', function () { }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { + return runners.noSession(config.regular, 4, async (logger, done) => { let resolved = 0; let rejected = 0; const a = new CancelablePromise((resolve, _reject) => { @@ -134,7 +136,7 @@ describe('Promises', function () { }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { + return runners.noSession(config.regular, 5, async (logger, done) => { let resolved = 0; let rejected = 0; const a = new CancelablePromise((_resolve, reject) => { @@ -169,7 +171,7 @@ describe('Promises', function () { }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { + return runners.noSession(config.regular, 6, async (logger, done) => { let resolved = 0; let rejected = 0; let canceled = 0; @@ -210,7 +212,7 @@ describe('Promises', function () { }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { + return runners.noSession(config.regular, 7, async (logger, done) => { let resolved = 0; let rejected = 0; let canceled = 0; @@ -261,7 +263,7 @@ describe('Promises', function () { }); it(config.regular.list[8], function () { - return runner(config.regular, 8, async (logger, done, collector) => { + return runners.noSession(config.regular, 8, async (logger, done) => { let resolved = 0; let rejected = 0; let canceled = 0; diff --git a/application/apps/rustcore/ts-bindings/spec/session.ranges.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.ranges.spec.ts index 96ffee9a9..a7ed56fd7 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.ranges.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.ranges.spec.ts @@ -1,15 +1,17 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IGrabbedElement } from 'platform/types/content'; -import { createSampleFile, finish, runner } from './common'; +import { createSampleFile, finish } from './common'; import { readConfigurationFile } from './config'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.ranges; function sum(from: number, to: number): number { @@ -22,105 +24,77 @@ function sum(from: number, to: number): number { describe('Grab ranges', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - const tmpobj = createSampleFile(1000, logger, (i: number) => `${i}\n`); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .catch(finish.bind(null, session, done)); - let grabbing: boolean = false; - events.StreamUpdated.subscribe((rows: number) => { - if (rows === 0 || grabbing) { - return; - } - grabbing = true; - Promise.all([ - stream - .grabRanges([{ from: 0, to: 99 }]) - .then((result: IGrabbedElement[]) => { - logger.debug('result of grab was: ' + JSON.stringify(result)); - expect( - result - .map((v) => parseInt(v.content, 10)) - .reduce((partialSum, a) => partialSum + a, 0), - ).toEqual(sum(0, 99)); - }), - stream - .grabRanges([ - { from: 0, to: 0 }, - { from: 10, to: 10 }, - ]) - .then((result: IGrabbedElement[]) => { - logger.debug('result of grab was: ' + JSON.stringify(result)); - expect(result.length).toEqual(2); - expect(parseInt(result[0].content, 10)).toEqual(0); - expect(parseInt(result[1].content, 10)).toEqual(10); - }), - stream - .grabRanges([ - { from: 0, to: 10 }, - { from: 99, to: 200 }, - { from: 299, to: 300 }, - { from: 599, to: 600 }, - ]) - .then((result: IGrabbedElement[]) => { - logger.debug('result of grab was: ' + JSON.stringify(result)); - expect( - result - .map((v) => parseInt(v.content, 10)) - .reduce((partialSum, a) => partialSum + a, 0), - ).toEqual( - sum(0, 10) + sum(99, 200) + sum(299, 300) + sum(599, 600), - ); - }), + return runners.withSession(config.regular, 1, async (logger, done, comps) => { + const tmpobj = createSampleFile(1000, logger, (i: number) => `${i}\n`); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .catch(finish.bind(null, comps.session, done)); + let grabbing: boolean = false; + comps.events.StreamUpdated.subscribe((rows: number) => { + if (rows === 0 || grabbing) { + return; + } + grabbing = true; + Promise.all([ + comps.stream + .grabRanges([{ from: 0, to: 99 }]) + .then((result: IGrabbedElement[]) => { + logger.debug('result of grab was: ' + JSON.stringify(result)); + expect( + result + .map((v) => parseInt(v.content, 10)) + .reduce((partialSum, a) => partialSum + a, 0), + ).toEqual(sum(0, 99)); + }), + comps.stream + .grabRanges([ + { from: 0, to: 0 }, + { from: 10, to: 10 }, + ]) + .then((result: IGrabbedElement[]) => { + logger.debug('result of grab was: ' + JSON.stringify(result)); + expect(result.length).toEqual(2); + expect(parseInt(result[0].content, 10)).toEqual(0); + expect(parseInt(result[1].content, 10)).toEqual(10); + }), + comps.stream + .grabRanges([ + { from: 0, to: 10 }, + { from: 99, to: 200 }, + { from: 299, to: 300 }, + { from: 599, to: 600 }, ]) - .then(() => { - finish(session, done); - }) - .catch((err: Error) => { - finish( - session, - done, - new Error( - `Fail to grab data due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + .then((result: IGrabbedElement[]) => { + logger.debug('result of grab was: ' + JSON.stringify(result)); + expect( + result + .map((v) => parseInt(v.content, 10)) + .reduce((partialSum, a) => partialSum + a, 0), + ).toEqual(sum(0, 10) + sum(99, 200) + sum(299, 300) + sum(599, 600)); + }), + ]) + .then(() => { + finish(comps.session, done); + }) + .catch((err: Error) => { + finish( + comps.session, + done, + new Error( + `Fail to grab data due error: ${ + err instanceof Error ? err.message : err + }`, + ), + ); }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + }); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.search.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.search.spec.ts index 630fdded6..ab4e218e6 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.search.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.search.spec.ts @@ -1,40 +1,22 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IGrabbedElement } from 'platform/types/content'; -import { finish, createSampleFile, performanceReport, setMeasurement, runner } from './common'; +import { finish, createSampleFile } from './common'; import { readConfigurationFile } from './config'; -import * as os from 'os'; -import * as path from 'path'; +import * as runners from './runners'; const config = readConfigurationFile().get().tests.search; describe('Search', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[1]); - const stream = session.getStream(); - const search = session.getSearch(); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } + return runners.withSession(config.regular, 1, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, @@ -45,7 +27,7 @@ describe('Search', function () { : `some line data\n` }`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -55,7 +37,7 @@ describe('Search', function () { .sterilized(), ) .on('processing', () => { - search + comps.search .search([ { filter: 'match', @@ -63,11 +45,11 @@ describe('Search', function () { }, ]) .then((_) => { - search + comps.search .getMap(54) .then((map) => { logger.verbose(map); - search + comps.search .grab(0, 11) .then((result: IGrabbedElement[]) => { expect(result.map((i) => i.content)).toEqual([ @@ -103,7 +85,7 @@ describe('Search', function () { [390, 9, 400], [600, 11, 600], ].map((data) => { - return search + return comps.search .getNearest(data[0]) .then((nearest) => { expect(typeof nearest).toEqual( @@ -122,14 +104,14 @@ describe('Search', function () { }), ) .then(() => { - search + comps.search .len() .then((len: number) => { expect(len).toEqual(55); expect( searchStreamUpdated, ).toEqual(true); - stream + comps.stream .getIndexedRanges() .then((ranges) => { expect( @@ -153,27 +135,27 @@ describe('Search', function () { ranges[i].to, ).toEqual(i * 100); } - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, err, ); }); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -185,46 +167,20 @@ describe('Search', function () { ); }); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let searchStreamUpdated = false; - events.SearchUpdated.subscribe((event) => { + comps.events.SearchUpdated.subscribe((event) => { searchStreamUpdated = true; }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[2]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - if (search instanceof Error) { - finish(session, done, search); - return; - } + return runners.withSession(config.regular, 2, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, @@ -239,7 +195,7 @@ describe('Search', function () { : `some line data\n` }`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -249,7 +205,7 @@ describe('Search', function () { .sterilized(), ) .on('processing', () => { - search + comps.search .search([ { filter: 'match A', @@ -265,7 +221,7 @@ describe('Search', function () { }, ]) .then((result) => { - search + comps.search .grab(0, 11) .then((result: IGrabbedElement[]) => { expect(result.map((i) => i.content)).toEqual([ @@ -301,7 +257,7 @@ describe('Search', function () { [55, 7, 50], [190, 10, 200], ].map((data) => { - return search + return comps.search .getNearest(data[0]) .then((nearest) => { expect(typeof nearest).toEqual( @@ -320,23 +276,23 @@ describe('Search', function () { }), ) .then(() => { - search + comps.search .len() .then((len: number) => { expect(len).toEqual(111); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -346,47 +302,20 @@ describe('Search', function () { ); }); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[3]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - if (search instanceof Error) { - finish(session, done, search); - return; - } - + return runners.withSession(config.regular, 3, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, (i: number) => `[${i}]:: some line data\n`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -396,7 +325,7 @@ describe('Search', function () { .sterilized(), ) .on('processing', () => { - search + comps.search .search([ { filter: 'not relevant search', @@ -405,43 +334,16 @@ describe('Search', function () { ]) .then((found) => { expect(found).toEqual(0); - finish(session, done); + finish(comps.session, done); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[4]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - if (search instanceof Error) { - finish(session, done, search); - return; - } - + return runners.withSession(config.regular, 4, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, @@ -452,7 +354,7 @@ describe('Search', function () { : `some line data\n` }`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -462,7 +364,7 @@ describe('Search', function () { .sterilized(), ) .on('processing', () => { - search + comps.search .search([ { filter: 'match', @@ -471,10 +373,10 @@ describe('Search', function () { ]) .then((_) => { // search results available on rust side - search + comps.search .getMap(54) .then((map) => { - search + comps.search .grab(0, 11) .then((result: IGrabbedElement[]) => { expect(result.map((i) => i.content)).toEqual([ @@ -510,7 +412,7 @@ describe('Search', function () { [390, 9, 400], [600, 11, 600], ].map((data) => { - return search + return comps.search .getNearest(data[0]) .then((nearest) => { expect(typeof nearest).toEqual( @@ -529,23 +431,23 @@ describe('Search', function () { }), ) .then(() => { - search + comps.search .len() .then((len: number) => { expect(len).toEqual(55); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -557,42 +459,16 @@ describe('Search', function () { ); }); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[5]); - const stream = session.getStream(); - const search = session.getSearch(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - if (search instanceof Error) { - finish(session, done, search); - return; - } + return runners.withSession(config.regular, 5, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, @@ -603,7 +479,7 @@ describe('Search', function () { : `some line matchmatchmatch data\n` }`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -613,7 +489,7 @@ describe('Search', function () { .sterilized(), ) .on('processing', () => { - search + comps.search .search([ { filter: 'match', @@ -622,10 +498,10 @@ describe('Search', function () { ]) .then((_) => { // search results available on rust side - search + comps.search .getMap(54) .then((map) => { - search + comps.search .grab(0, 11) .then((result: IGrabbedElement[]) => { expect(result.map((i) => i.content)).toEqual([ @@ -661,7 +537,7 @@ describe('Search', function () { [390, 9, 400], [600, 11, 600], ].map((data) => { - return search + return comps.search .getNearest(data[0]) .then((nearest) => { expect(typeof nearest).toEqual( @@ -680,23 +556,23 @@ describe('Search', function () { }), ) .then(() => { - search + comps.search .len() .then((len: number) => { expect(len).toEqual(55); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -708,45 +584,16 @@ describe('Search', function () { ); }); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); + .catch(finish.bind(null, comps.session, done)); }); }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[6]); - const stream = session.getStream(); - const search = session.getSearch(); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } + return runners.withSession(config.regular, 6, async (logger, done, comps) => { const tmpobj = createSampleFile( 501, logger, @@ -757,7 +604,7 @@ describe('Search', function () { : `some line data\n` }`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -767,7 +614,7 @@ describe('Search', function () { .sterilized(), ) .on('processing', () => { - search + comps.search .search([ { filter: 'match A', @@ -779,7 +626,7 @@ describe('Search', function () { }, ]) .then((_) => { - search + comps.search .getMap(501) .then((map) => { expect(map[0].length).toEqual(2); @@ -788,7 +635,7 @@ describe('Search', function () { expect(map[500][0][0]).toEqual(0); expect(map[500][1][0]).toEqual(1); logger.verbose(map); - search + comps.search .grab(0, 11) .then((result: IGrabbedElement[]) => { expect(result.map((i) => i.content)).toEqual([ @@ -817,22 +664,22 @@ describe('Search', function () { 400, // 9 500, // 10 ]); - search + comps.search .len() .then((len: number) => { expect(len).toEqual(11); expect(searchStreamUpdated).toEqual( true, ); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -844,49 +691,20 @@ describe('Search', function () { ); }); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let searchStreamUpdated = false; - events.SearchUpdated.subscribe((event) => { + comps.events.SearchUpdated.subscribe((event) => { searchStreamUpdated = true; }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[7]); - const stream = session.getStream(); - const search = session.getSearch(); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); - } + return runners.withSession(config.regular, 7, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, @@ -905,7 +723,7 @@ describe('Search', function () { : `some line data\n` }`, ); - stream + comps.stream .observe( new Factory.File() .asText() @@ -918,7 +736,7 @@ describe('Search', function () { const calls = ['match A', 'match D', 'match C', 'match B']; let canceled = 0; calls.forEach((filter) => { - search + comps.search .search([ { filter, @@ -927,7 +745,7 @@ describe('Search', function () { ]) .then((_) => { expect(canceled).toEqual(3); - search + comps.search .grab(0, 16) .then((result: IGrabbedElement[]) => { expect(result.map((i) => i.content)).toEqual([ @@ -952,20 +770,20 @@ describe('Search', function () { 0, 300, 600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000, 3300, 3600, 3900, 4200, 4500, ]); - search + comps.search .len() .then((len: number) => { expect(len).toEqual(17); expect(searchStreamUpdated).toEqual(true); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { - finish(session, done, err); + finish(comps.session, done, err); }); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -979,27 +797,15 @@ describe('Search', function () { canceled += 1; }) .catch((err: Error) => { - finish(session, done); + finish(comps.session, done); }); }); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); let searchStreamUpdated = false; - events.SearchUpdated.subscribe((event) => { + comps.events.SearchUpdated.subscribe((event) => { searchStreamUpdated = true; }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); }); }); }); diff --git a/application/apps/rustcore/ts-bindings/spec/session.stream.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.stream.spec.ts index 93a1725e8..b4a2c4ffe 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.stream.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.stream.spec.ts @@ -1,19 +1,20 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; +import { Factory } from '../src/api/session'; import { IGrabbedElement } from 'platform/types/content'; -import { createSampleFile, finish, performanceReport, setMeasurement, runner } from './common'; +import { createSampleFile, finish } from './common'; import { readConfigurationFile } from './config'; import { utils } from 'platform/log'; import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; +import * as runners from './runners'; const config = readConfigurationFile().get().tests.stream; // ================================== IMPORTANT ================================== @@ -31,27 +32,14 @@ if (process.platform === 'win32') { } else { describe('Stream', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 1, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, (i: number) => `some line data: ${i}\n`, ); - stream + comps.stream .observe( new Factory.Stream() .asText() @@ -63,12 +51,12 @@ if (process.platform === 'win32') { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); - events.StreamUpdated.subscribe((rows: number) => { + .catch(finish.bind(null, comps.session, done)); + comps.events.StreamUpdated.subscribe((rows: number) => { if (rows < 5000) { return; } - stream + comps.stream .grab(500, 7) .then((result: IGrabbedElement[]) => { logger.debug('result of grab was: ' + JSON.stringify(result)); @@ -81,11 +69,11 @@ if (process.platform === 'win32') { 'some line data: 505', 'some line data: 506', ]); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -99,27 +87,14 @@ if (process.platform === 'win32') { }); it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 2, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, (i: number) => `some line data: ${i}\n`, ); const lifeCycleEvents: string[] = []; - stream + comps.stream .observe( new Factory.Stream() .asText() @@ -137,34 +112,21 @@ if (process.platform === 'win32') { .on('processing', () => { lifeCycleEvents.push('processing'); }) - .catch(finish.bind(null, session, done)); - events.StreamUpdated.subscribe((rows: number) => { + .catch(finish.bind(null, comps.session, done)); + comps.events.StreamUpdated.subscribe((rows: number) => { if (rows < 5000) { return; } expect(lifeCycleEvents).toEqual(['confirmed', 'processing']); - finish(session, done); + finish(comps.session, done); }); }); }); it(config.regular.list[3], function () { - return runner(config.regular, 3, async (logger, done, collector) => { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 3, async (logger, done, comps) => { const lifeCycleEvents: string[] = []; - stream + comps.stream .observe( new Factory.Stream() .asText() @@ -183,38 +145,25 @@ if (process.platform === 'win32') { lifeCycleEvents.push('processing'); }) .then(() => { - finish(session, done, new Error(`Shound not be resolved`)); + finish(comps.session, done, new Error(`Shound not be resolved`)); }) .catch((err: Error) => { // Event 'processing' should not come, because stream was created (confirmed), but // it cannot be processed as soon as command is invalid expect(lifeCycleEvents).toEqual(['confirmed']); - finish(session, done); + finish(comps.session, done); }); }); }); it(config.regular.list[4], function () { - return runner(config.regular, 4, async (logger, done, collector) => { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 4, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, (i: number) => `some line data: ${i}\n`, ); - stream + comps.stream .observe( new Factory.Stream() .asText() @@ -226,9 +175,9 @@ if (process.platform === 'win32') { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); const append = () => { - stream + comps.stream .len() .then((len) => { expect(len).toBe(5000); @@ -243,7 +192,7 @@ if (process.platform === 'win32') { }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -255,7 +204,7 @@ if (process.platform === 'win32') { }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -265,7 +214,7 @@ if (process.platform === 'win32') { ); }); }; - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { if (rows === 5000) { append(); return; @@ -273,7 +222,7 @@ if (process.platform === 'win32') { if (rows < 10000) { return; } - stream + comps.stream .grab(500, 7) .then((result: IGrabbedElement[]) => { logger.debug('result of grab was: ' + JSON.stringify(result)); @@ -286,11 +235,11 @@ if (process.platform === 'win32') { 'some line data: 505', 'some line data: 506', ]); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -304,31 +253,13 @@ if (process.platform === 'win32') { }); it(config.regular.list[5], function () { - return runner(config.regular, 5, async (logger, done, collector) => { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const search = session.getSearch(); - if (search instanceof Error) { - finish(session, done, search); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 5, async (logger, done, comps) => { const tmpobj = createSampleFile( 500, logger, (i: number) => `some line data: ${i}\n`, ); - stream + comps.stream .observe( new Factory.Stream() .asText() @@ -340,11 +271,11 @@ if (process.platform === 'win32') { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); const filter = 'match A'; let expectedMatchesCount = 0; const append = () => { - stream + comps.stream .len() .then((len) => { expect(len).toBe(500); @@ -367,7 +298,7 @@ if (process.platform === 'win32') { }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -379,7 +310,7 @@ if (process.platform === 'win32') { }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ @@ -389,9 +320,9 @@ if (process.platform === 'win32') { ); }); }; - events.StreamUpdated.subscribe((rows: number) => { + comps.events.StreamUpdated.subscribe((rows: number) => { if (rows === 500) { - search + comps.search .search([ { filter, @@ -405,7 +336,7 @@ if (process.platform === 'win32') { }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to search due error: ${ @@ -417,37 +348,24 @@ if (process.platform === 'win32') { return; } }); - events.SearchUpdated.subscribe((updated) => { + comps.events.SearchUpdated.subscribe((updated) => { if (updated.found <= 0) { return; } expect(updated.found).toBe(expectedMatchesCount); - finish(session, done); + finish(comps.session, done); }); }); }); it(config.regular.list[6], function () { - return runner(config.regular, 6, async (logger, done, collector) => { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 6, async (logger, done, comps) => { const tmpobj = createSampleFile( 5000, logger, (i: number) => `some line data: ${i}\n`, ); - const tailing = stream + const tailing = comps.stream .observe( new Factory.Stream() .asText() @@ -459,14 +377,14 @@ if (process.platform === 'win32') { .get() .sterilized(), ) - .catch(finish.bind(null, session, done)); - events.StreamUpdated.subscribe((rows: number) => { + .catch(finish.bind(null, comps.session, done)); + comps.events.StreamUpdated.subscribe((rows: number) => { if (rows < 5000) { return; } tailing .canceled(() => { - finish(session, done); + finish(comps.session, done); }) .abort(); }); @@ -474,20 +392,7 @@ if (process.platform === 'win32') { }); it(config.regular.list[7], function () { - return runner(config.regular, 7, async (logger, done, collector) => { - const session = await Session.create(); - // Set provider into debug mode - session.debug(true); - const stream = session.getStream(); - if (stream instanceof Error) { - finish(session, done, stream); - return; - } - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } + return runners.withSession(config.regular, 7, async (logger, done, comps) => { const filename = path.join(os.tmpdir(), 'chipmunk_test_file.log'); // Create or overwrite file fs.writeFileSync(filename, ''); @@ -500,14 +405,14 @@ if (process.platform === 'win32') { } try { // Send first message - await stream.sde(sed.uuid(), { WriteText: `${TEST_LINES[0]}\n` }); + await comps.stream.sde(sed.uuid(), { WriteText: `${TEST_LINES[0]}\n` }); // Send second message - await stream.sde(sed.uuid(), { WriteText: `${TEST_LINES[1]}\n` }); + await comps.stream.sde(sed.uuid(), { WriteText: `${TEST_LINES[1]}\n` }); } catch (e) { - finish(session, done, new Error(utils.error(e))); + finish(comps.session, done, new Error(utils.error(e))); } }; - const sed = stream + const sed = comps.stream .observe( new Factory.Stream() .asText() @@ -522,9 +427,9 @@ if (process.platform === 'win32') { .on('processing', (e) => { procceed(); }) - .catch(finish.bind(null, session, done)); + .catch(finish.bind(null, comps.session, done)); // Tail file - const _tail = stream + const _tail = comps.stream .observe( new Factory.Stream() .asText() @@ -539,12 +444,12 @@ if (process.platform === 'win32') { .on('processing', () => { procceed(); }) - .catch(finish.bind(null, session, done)); - events.StreamUpdated.subscribe((rows: number) => { + .catch(finish.bind(null, comps.session, done)); + comps.events.StreamUpdated.subscribe((rows: number) => { if (rows < 4) { return; } - stream + comps.stream .grab(0, 4) .then((result: IGrabbedElement[]) => { logger.debug('result of grab was: ' + JSON.stringify(result)); @@ -560,11 +465,11 @@ if (process.platform === 'win32') { expect(result.filter((i) => i.content === TEST_LINES[1]).length).toBe( 2, ); - finish(session, done); + finish(comps.session, done); }) .catch((err: Error) => { finish( - session, + comps.session, done, new Error( `Fail to grab data due error: ${ diff --git a/application/apps/rustcore/ts-bindings/spec/session.values.spec.ts b/application/apps/rustcore/ts-bindings/spec/session.values.spec.ts index 971aaec9d..14baed9e5 100644 --- a/application/apps/rustcore/ts-bindings/spec/session.values.spec.ts +++ b/application/apps/rustcore/ts-bindings/spec/session.values.spec.ts @@ -1,67 +1,95 @@ // tslint:disable - // We need to provide path to TypeScript types definitions /// /// + import { initLogger } from './logger'; initLogger(); -import { Session, Factory } from '../src/api/session'; -import { finish, createSampleFile, appendToSampleFile, runner } from './common'; +import { Factory } from '../src/api/session'; +import { finish, createSampleFile, appendToSampleFile } from './common'; import { readConfigurationFile } from './config'; +import * as runners from './runners'; + const config = readConfigurationFile().get().tests.values; const MAX_DATASET_LEN = 65000; describe('Values', function () { it(config.regular.list[1], function () { - return runner(config.regular, 1, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[1]); - const stream = session.getStream(); - const search = session.getSearch(); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); + return runners.withSession(config.regular, 1, async (logger, done, comps) => { + let sum = 0; + const tmpobj = createSampleFile(5000, logger, (i: number) => { + if (i % 100 === 0 || i <= 5) { + sum += i; + return `[${i}]:: some data CPU=${i}% line data\n`; + } else { + return `[${i}]:: some line data\n`; + } + }); + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search.values([`CPU=(\\d{1,})`]).catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + comps.events.SearchValuesUpdated.subscribe((map) => { + if (map === null) { + // Before get results rustcore should inform FE about dropping results. return; } - if (stream instanceof Error) { - return finish(session, done, stream); + comps.search + .getValues(MAX_DATASET_LEN) + .then((data) => { + let control = 0; + data[0].forEach((pair) => { + control += pair[3]; + }); + expect(control).toEqual(sum); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + }); + }); + }); + it(config.regular.list[2], function () { + return runners.withSession(config.regular, 2, async (logger, done, comps) => { + let sum = 0; + const tmpobj = createSampleFile(5000, logger, (i: number) => { + if (i % 100 === 0 || i <= 5) { + sum += i; + return `[${i}]:: some data CPU=${i}% line data\n`; + } else { + return `[${i}]:: some line data\n`; } - if (search instanceof Error) { - return finish(session, done, search); + }); + let iteration = 0; + comps.stream + .observe( + new Factory.File() + .asText() + .type(Factory.FileType.Text) + .file(tmpobj.name) + .get() + .sterilized(), + ) + .on('processing', () => { + comps.search.values([`CPU=(\\d{1,})`]).catch(finish.bind(null, comps.session, done)); + }) + .catch(finish.bind(null, comps.session, done)); + comps.events.SearchValuesUpdated.subscribe((map) => { + if (map === null) { + // Before get results rustcore should inform FE about dropping results. + return; } - let sum = 0; - const tmpobj = createSampleFile(5000, logger, (i: number) => { - if (i % 100 === 0 || i <= 5) { - sum += i; - return `[${i}]:: some data CPU=${i}% line data\n`; - } else { - return `[${i}]:: some line data\n`; - } - }); - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .values([`CPU=(\\d{1,})`]) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - events.SearchValuesUpdated.subscribe((map) => { - if (map === null) { - // Before get results rustcore should inform FE about dropping results. - return; - } - search + if (iteration === 0) { + comps.search .getValues(MAX_DATASET_LEN) .then((data) => { let control = 0; @@ -69,123 +97,33 @@ describe('Values', function () { control += pair[3]; }); expect(control).toEqual(sum); - finish(session, done); + const offset = 5000; + appendToSampleFile(tmpobj, 5000, logger, (i: number) => { + if (i % 100 === 0 || i <= 5) { + sum += i + offset; + return `[${i}]:: some data CPU=${i + offset}% line data\n`; + } else { + return `[${i}]:: some line data\n`; + } + }); }) - .catch(finish.bind(null, session, done)); - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); - }); - }); - }); - it(config.regular.list[2], function () { - return runner(config.regular, 2, async (logger, done, collector) => { - Session.create() - .then((session: Session) => { - // Set provider into debug mode - session.debug(true, config.regular.list[2]); - const stream = session.getStream(); - const search = session.getSearch(); - const events = session.getEvents(); - if (events instanceof Error) { - finish(session, done, events); - return; - } - if (stream instanceof Error) { - return finish(session, done, stream); - } - if (search instanceof Error) { - return finish(session, done, search); + .catch(finish.bind(null, comps.session, done)); + iteration += 1; + } else if (iteration === 1) { + comps.search + .getValues(MAX_DATASET_LEN) + .then((data) => { + let control = 0; + data[0].forEach((pair) => { + control += pair[3]; + }); + expect(control).toEqual(sum); + finish(comps.session, done); + }) + .catch(finish.bind(null, comps.session, done)); + } else { + expect(iteration).toEqual(1); } - let sum = 0; - const tmpobj = createSampleFile(5000, logger, (i: number) => { - if (i % 100 === 0 || i <= 5) { - sum += i; - return `[${i}]:: some data CPU=${i}% line data\n`; - } else { - return `[${i}]:: some line data\n`; - } - }); - let iteration = 0; - stream - .observe( - new Factory.File() - .asText() - .type(Factory.FileType.Text) - .file(tmpobj.name) - .get() - .sterilized(), - ) - .on('processing', () => { - search - .values([`CPU=(\\d{1,})`]) - .catch(finish.bind(null, session, done)); - }) - .catch(finish.bind(null, session, done)); - events.SearchValuesUpdated.subscribe((map) => { - if (map === null) { - // Before get results rustcore should inform FE about dropping results. - return; - } - if (iteration === 0) { - search - .getValues(MAX_DATASET_LEN) - .then((data) => { - let control = 0; - data[0].forEach((pair) => { - control += pair[3]; - }); - expect(control).toEqual(sum); - const offset = 5000; - appendToSampleFile(tmpobj, 5000, logger, (i: number) => { - if (i % 100 === 0 || i <= 5) { - sum += i + offset; - return `[${i}]:: some data CPU=${ - i + offset - }% line data\n`; - } else { - return `[${i}]:: some line data\n`; - } - }); - }) - .catch(finish.bind(null, session, done)); - iteration += 1; - } else if (iteration === 1) { - search - .getValues(MAX_DATASET_LEN) - .then((data) => { - let control = 0; - data[0].forEach((pair) => { - control += pair[3]; - }); - expect(control).toEqual(sum); - finish(session, done); - }) - .catch(finish.bind(null, session, done)); - } else { - expect(iteration).toEqual(1); - } - }); - }) - .catch((err: Error) => { - finish( - undefined, - done, - new Error( - `Fail to create session due error: ${ - err instanceof Error ? err.message : err - }`, - ), - ); }); }); });