Skip to content

Commit

Permalink
Adds tests for the periodic updater
Browse files Browse the repository at this point in the history
  • Loading branch information
scourgemancer authored and nicholastmosher committed Feb 19, 2020
1 parent f2a9bbe commit aae859e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { app } from './server';
import { updateData, startPeriodicUpdates } from './utils/data_fetcher';
import { updateData, startPeriodicallyCalling } from './utils/data_fetcher';

updateData();
const default_interval: string = (1000 * 60 * 60 * 24).toString();
const interval: string = process.env["UPDATE_INTERVAL"] || default_interval;
startPeriodicUpdates(parseInt(interval));
startPeriodicallyCalling(updateData, parseInt(interval));

const PORT = process.env.PORT || 3000;

Expand Down
19 changes: 19 additions & 0 deletions src/utils/__tests__/test_data_fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {startPeriodicallyCalling} from "../data_fetcher";

describe("Data updating functions", () => {

it("should update once for each period", () => {
let updateData = jest.fn();
startPeriodicallyCalling(updateData, 300, new Date());
setTimeout(() => expect(updateData).toHaveBeenCalledTimes(3), 1000);
});

it("should only start updating after the start date", () => {
let updateData = jest.fn();
const startDate = new Date();
startDate.setMilliseconds(startDate.getMilliseconds() + 500);
startPeriodicallyCalling(updateData, 200, startDate);
setTimeout(() => expect(updateData).toHaveBeenCalledTimes(0), 400);
setTimeout(() => expect(updateData).toHaveBeenCalledTimes(2), 1000);
});
});
31 changes: 17 additions & 14 deletions src/utils/data_fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@ let mostRecentGeneMap: GeneMap;
let mostRecentAnnotations: IAnnotation[];
let mostRecentGroupedAnnotations: GroupedAnnotations<Set<IGene>>;

export const updateData = () => {
export const getGeneMap = (): GeneMap => { return mostRecentGeneMap; };

export const getAnnotations = (): IAnnotation[] => { return mostRecentAnnotations; };

export const getGroupedAnnotations = (): GroupedAnnotations<Set<IGene>> => { return mostRecentGroupedAnnotations; };

export const downloadData = (dataUrl: string = "http://current.geneontology.org/annotations/tair.gaf.gz") => {
const { execSync } = require("child_process");
const stdout = execSync("wget http://current.geneontology.org/annotations/tair.gaf.gz ; gunzip tair.gaf.gz");
const stdout = execSync("wget " + dataUrl + " ; gunzip tair.gaf.gz");
}

export const updateData = () => {
downloadData();
const { geneMap, annotations, groupedAnnotations } = readData();
mostRecentGeneMap = geneMap;
mostRecentAnnotations = annotations;
mostRecentGroupedAnnotations = groupedAnnotations;
};

export const startPeriodicUpdates = (interval: number = (1000 * 60 * 60 * 24), startDate: Date = getTomorrowMorning()) => {
export const startPeriodicallyCalling = (fn: (...args: any[]) => void, interval: number = (1000 * 60 * 60 * 24), startDate: Date = getTomorrowMorning()) => {
const now = new Date();
if (sameTimeOfDay(startDate, now)) {
setInterval(updateData, interval);
if (isSameTimeOfDay(startDate, now)) {
setInterval(fn, interval);
} else {
let difference = startDate.getTime() - now.getTime();
if (difference > 0) {
setTimeout(startPeriodicUpdates, difference, interval, startDate);
setTimeout(startPeriodicallyCalling, difference, fn, interval, startDate);
}
setInterval(updateData, interval); // Just begins the updates now if the startDate is in the past
setInterval(fn, interval); // Just begins the updates now if the startDate is in the past
}
};

Expand All @@ -36,12 +45,6 @@ const getTomorrowMorning = (): Date => {
return tomorrowMorning;
};

const sameTimeOfDay = (time1, time2) => {
const isSameTimeOfDay = (time1, time2) => {
return time1.getHours() === time2.getHours() && time1.getMinutes() === time2.getMinutes();
}

export const getGeneMap = (): GeneMap => { return mostRecentGeneMap; };

export const getAnnotations = (): IAnnotation[] => { return mostRecentAnnotations; };

export const getGroupedAnnotations = (): GroupedAnnotations<Set<IGene>> => { return mostRecentGroupedAnnotations; };

0 comments on commit aae859e

Please sign in to comment.