Skip to content

testkit-backend: Enabling skip different tests for browser and node #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/testkit-backend/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import commonjs from '@rollup/plugin-commonjs'
import polyfillNode from 'rollup-plugin-polyfill-node'
import injectProcessEnv from 'rollup-plugin-inject-process-env'

function getDescriptor () {
const currentDescriptor = process.env.DRIVER_DESCRIPTOR || ''
return currentDescriptor + ',browser'
}

export default {
input: 'src/index.js',
output: {
Expand All @@ -22,6 +27,7 @@ export default {
...process.env,
TEST_ENVIRONMENT: 'LOCAL',
CHANNEL_TYPE: 'WEBSOCKET',
DRIVER_DESCRIPTOR: getDescriptor(),
BACKEND_PORT: process.env.WEB_SERVER_PORT || 8000
})
]
Expand Down
7 changes: 6 additions & 1 deletion packages/testkit-backend/src/context.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export default class Context {
constructor () {
constructor (shouldRunTest) {
this._id = 0
this._drivers = {}
this._sessions = {}
this._txs = {}
this._resolverRequests = {}
this._resultObservers = {}
this._errors = {}
this._shouldRunTest = shouldRunTest
}

addDriver (driver) {
Expand Down Expand Up @@ -98,6 +99,10 @@ export default class Context {
return Object.values(this._txs).filter(tx => tx.sessionId === sessionId)
}

getShouldRunTestFunction() {
return this._shouldRunTest
}

_add (map, object) {
this._id++
map[this._id] = object
Expand Down
5 changes: 3 additions & 2 deletions packages/testkit-backend/src/controller/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import Controller from './interface'
*/
export default class LocalController extends Controller {

constructor(requestHandlers = {}) {
constructor(requestHandlers = {}, shouldRunTest = () => {}) {
super()
this._requestHandlers = requestHandlers
this._shouldRunTest = shouldRunTest
this._contexts = new Map()
}

openContext (contextId) {
this._contexts.set(contextId, new Context())
this._contexts.set(contextId, new Context(this._shouldRunTest))
}

closeContext (contextId) {
Expand Down
13 changes: 11 additions & 2 deletions packages/testkit-backend/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Backend from './backend'
import { SocketChannel, WebSocketChannel } from './channel'
import { LocalController, RemoteController } from './controller'
import { getShouldRunTest } from './skipped-tests'
import * as REQUEST_HANDLERS from './request-handlers'

/**
Expand All @@ -11,6 +12,11 @@ function main( ) {
const channelType = process.env.CHANNEL_TYPE || 'SOCKET'
const backendPort = process.env.BACKEND_PORT || 9876
const webserverPort = process.env.WEB_SERVER_PORT || 8000
const driverDescriptor = process.env.DRIVER_DESCRIPTOR || ''
const driverDescriptorList = driverDescriptor
.split(',').map(s => s.trim().toLowerCase())

const shouldRunTest = getShouldRunTest(driverDescriptorList)

const newChannel = () => {
if ( channelType.toUpperCase() === 'WEBSOCKET' ) {
Expand All @@ -24,7 +30,7 @@ function main( ) {
if ( testEnviroment.toUpperCase() === 'REMOTE' ) {
return new RemoteController(webserverPort)
}
return new LocalController(REQUEST_HANDLERS)
return new LocalController(REQUEST_HANDLERS, shouldRunTest)
}

const backend = new Backend(newController, newChannel)
Expand All @@ -39,7 +45,10 @@ function main( ) {
process.on('SIGINT', process.exit.bind(process));
process.on('SIGUSR1', process.exit.bind(process));
process.on('SIGUSR2', process.exit.bind(process));
process.on('uncaughtException', process.exit.bind(process));
process.on('uncaughtException', exception => {
console.error('UncaughtException', exception)
process.exit()
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/testkit-backend/src/request-handlers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import neo4j from './neo4j'
import ResultObserver from './result-observer.js'
import { cypherToNative, nativeToCypher } from './cypher-native-binders.js'
import { shouldRunTest } from './skipped-tests'
import tls from 'tls'

const SUPPORTED_TLS = (() => {
Expand Down Expand Up @@ -302,7 +301,8 @@ export function SessionWriteTransaction (context, data, wire) {
.catch(error => wire.writeError(error))
}

export function StartTest (_, { testName }, wire) {
export function StartTest (context, { testName }, wire) {
const shouldRunTest = context.getShouldRunTestFunction()
shouldRunTest(testName, {
onRun: () => wire.writeResponse('RunTest', null),
onSkip: reason => wire.writeResponse('SkipTest', { reason })
Expand Down
13 changes: 13 additions & 0 deletions packages/testkit-backend/src/skipped-tests/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import skip, { ifStartsWith } from './skip'
const skippedTests = [
skip(
'Stub Tests not implemented for browser',
ifStartsWith('stub')
),
skip(
'TLS Tests not implemented for browwer',
ifStartsWith('tls')
)
]

export default skippedTests
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
function ifEndsWith (suffix) {
return testName => testName.endsWith(suffix)
}

function ifStartsWith (prefix) {
return testName => testName.startsWith(prefix)
}

function ifEquals (expectedName) {
return testName => testName === expectedName
}

function or () {
return testName => [...arguments].find(predicate => predicate(testName))
}

function skip (reason, ...predicate) {
return { reason, predicate: or(...predicate) }
}
import skip, { ifEquals, ifEndsWith } from './skip'

const skippedTests = [
skip(
Expand Down Expand Up @@ -109,8 +91,4 @@ const skippedTests = [
)
]

export function shouldRunTest (testName, { onRun, onSkip }) {
const { reason } =
skippedTests.find(({ predicate }) => predicate(testName)) || {}
!reason ? onRun() : onSkip(reason)
}
export default skippedTests
19 changes: 19 additions & 0 deletions packages/testkit-backend/src/skipped-tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import commonSkippedTests from './common'
import browserSkippedTests from './browser'

const skippedTestsByContext = new Map([
['browser', browserSkippedTests]
])

export function getShouldRunTest (contexts) {
const skippedTests = contexts
.filter(context => skippedTestsByContext.has(context))
.map(context => skippedTestsByContext.get(context))
.reduce((previous, current) => [ ...previous, ...current ], commonSkippedTests)

return (testName, { onRun, onSkip }) => {
const { reason } =
skippedTests.find(({ predicate }) => predicate(testName)) || {}
!reason ? onRun() : onSkip(reason)
}
}
21 changes: 21 additions & 0 deletions packages/testkit-backend/src/skipped-tests/skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function ifEndsWith (suffix) {
return testName => testName.endsWith(suffix)
}

export function ifStartsWith (prefix) {
return testName => testName.startsWith(prefix)
}

export function ifEquals (expectedName) {
return testName => testName === expectedName
}

export function or () {
return testName => [...arguments].find(predicate => predicate(testName))
}

export function skip (reason, ...predicate) {
return { reason, predicate: or(...predicate) }
}

export default skip