Skip to content

Commit

Permalink
Initialise library automatically and only run Xvfb on Lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
angelsl committed Aug 17, 2020
1 parent 36864f3 commit 62ec7ea
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build-grader.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Build package for AWS Lambda
on:
push:
branches:
- master
on: workflow_dispatch

jobs:
tsc:
Expand Down
66 changes: 33 additions & 33 deletions src/__tests__/test_rune.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
import { awsEventFactory } from "./helpers";
import { runAll } from "../index";
import { awsEventFactory } from './helpers'
import { runAll } from '../index'

const makeAwsEvent = awsEventFactory({
chapter: 2,
external: {
name: "RUNES",
name: 'RUNES',
symbols: [
"heart",
"square",
"nova",
"beside",
"stack",
"show",
"picture_mse",
"getReadyWebGLForCanvas",
],
'heart',
'square',
'nova',
'beside',
'stack',
'show',
'picture_mse',
'getReadyWebGLForCanvas'
]
},
globals: [],
});
globals: []
})

test("rune correct", async () => {
test('rune correct', async () => {
const results = await runAll(
makeAwsEvent({
prependProgram: "getReadyWebGLForCanvas('2d');",
studentProgram: "",
postpendProgram: "",
prependProgram: '',
studentProgram: '',
postpendProgram: '',
testcases: [
{
program: `
picture_mse(show(square), show(square)) < 0.01;
`,
answer: "true",
score: 1,
answer: 'true',
score: 1
},
{
program: `
picture_mse(show(stack(square, square)), show(beside(square, square))) < 0.01;
`,
answer: "true",
score: 1,
answer: 'true',
score: 1
},
{
program: `
picture_mse(show(square), show(beside(square, square))) < 0.01;
`,
answer: "true",
score: 1,
answer: 'true',
score: 1
},
{
program: `
picture_mse(show(heart), show(beside(square, square))) < 0.01;
`,
answer: "false",
score: 1,
answer: 'false',
score: 1
},
{
program: `
picture_mse(show(heart), show(nova)) < 0.01;
`,
answer: "false",
score: 1,
},
],
answer: 'false',
score: 1
}
]
})
);
)
expect(results).toMatchInlineSnapshot(`
Object {
"results": Array [
Expand All @@ -90,5 +90,5 @@ picture_mse(show(heart), show(nova)) < 0.01;
],
"totalScore": 5,
}
`);
});
`)
})
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
importBuiltins
} from 'js-slang/dist/createContext'
import { ChildProcess } from 'child_process'
import { setupXvfb } from './setupXvfb'
import { setupLambdaXvfb } from './setupXvfb'

const externals = {}
const externals: any = {}
Object.assign(externals, require('./tree.js'))

const TIMEOUT_DURATION = process.env.TIMEOUT ? parseInt(process.env.TIMEOUT!, 10) : 3000 // in milliseconds
Expand Down Expand Up @@ -122,8 +122,9 @@ export const runAll = async (event: AwsEvent): Promise<Summary> => {
if (event.library && event.library.external) {
switch (event.library.external.name) {
case 'RUNES': {
xvfb = await setupXvfb()
xvfb = await setupLambdaXvfb()
Object.assign(externals, require('./graphics/webGLrune.js'))
externals.getReadyWebGLForCanvas('2d')
break
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/setupXvfb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const copyAsync = promisify(copyFile)
const sleep = promisify(setTimeout)
const statAsync = promisify(stat)

export async function setupXvfb() {
const IS_LAMBDA = !!process.env.AWS_LAMBDA_FUNCTION_NAME

export async function setupLambdaXvfb() {
if (!IS_LAMBDA) {
return null
}
await Promise.all([
copyBinary('/opt/bin/xkbcomp', '/tmp/xkbcomp'),
copyBinary('/opt/bin/Xvfb', '/tmp/Xvfb')
Expand Down

0 comments on commit 62ec7ea

Please sign in to comment.