Skip to content

Commit

Permalink
DEV: more tests and code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael O'Brien committed Aug 20, 2023
1 parent 9829598 commit 6d6e727
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 24 deletions.
35 changes: 35 additions & 0 deletions test/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
api.ts - Test misc api routines
*/
import {Schema, Client, CustomMetrics, log, Table, dump} from './utils/init'

// jest.setTimeout(7200 * 1000)

const TableName = 'APITestTable'
const table = new Table({
name: TableName,
client: Client,
partial: true,
senselogs: log,
schema: Schema,
})

test('Create Table', async () => {
// This will create a local table
if (!(await table.exists())) {
await table.createTable()
expect(await table.exists()).toBe(true)
}
})

test('Test flush', async () => {
let metrics = new CustomMetrics({onetable: table})
await metrics.flush()
await CustomMetrics.flushAll()
await CustomMetrics.terminate()
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
})
56 changes: 55 additions & 1 deletion test/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('Create Table', async () => {
})

test('Test', async () => {
let metrics = new CustomMetrics({onetable: table, owner: 'service', log: true})
let metrics = new CustomMetrics({onetable: table, log: true})
let timestamp = new Date(2000, 0, 1).getTime()
let span = DefaultSpans[0]
let interval = span.period / span.samples
Expand Down Expand Up @@ -51,6 +51,60 @@ test('Test', async () => {
expect(r.points[0].count).toBe(4)
})

test('Test elapsed buffers', async () => {
let metrics = new CustomMetrics({onetable: table, log: true})
let timestamp = new Date(2000, 0, 1).getTime()
let span = DefaultSpans[0]
let interval = span.period / span.samples

/*
Buffer some metrics and then flush
*/
for (let i = 0; i < 4; i++) {
let metric = await metrics.emit('myspace/test', 'BufferMetric', 1, [], {buffer: {elapsed: 1800}, timestamp})
expect(metric).toBeDefined()
expect(metric.metric).toBe('BufferMetric')
expect(metric.spans.length).toBe(1)
expect(metric.spans[0].points.length).toBe(1)
expect(metric.spans[0].points[0].count).toBe(i + 1)
timestamp += interval * 1000
}
/*
Emit again after long delay this should cause the prior buffer to be flushed
*/
timestamp += 86400 * 1000
let metric = await metrics.emit('myspace/test', 'BufferMetric', 7, [], {buffer: {elapsed: 1800}, timestamp})

let r = await metrics.query('myspace/test', 'BufferMetric', {}, 3600, 'avg', {timestamp})
expect(r).toBeDefined()
expect(r.metric).toBe('BufferMetric')
expect(r.namespace).toBe('myspace/test')
expect(r.period).toBe(3600)
expect(r.points).toBeDefined()
expect(r.points.length).toBe(1)
expect(r.points[0].value).toBe(2.2)
expect(r.points[0].count).toBe(5)
})

test('Test buffer API', async () => {
let metrics = new CustomMetrics({onetable: table})
let metric = await metrics.emit('myspace/test', 'CoverageMetric', 1, [], {buffer: {sum: 1}})
expect(metric).toBeDefined()

metrics = new CustomMetrics({onetable: table, buffer: {elapsed: 1800}})
metric = await metrics.emit('myspace/test', 'CoverageMetric', 1, [])
expect(metric).toBeDefined()

metrics = new CustomMetrics({onetable: table, buffer: {elapsed: 1800}})
metric = await metrics.emit('myspace/test', 'CoverageMetric', 1, [])
expect(metric).toBeDefined()

metrics = new CustomMetrics({onetable: table})
metric = await metrics.emit('myspace/test', 'CoverageMetric', 1, [], {buffer: {count: 1}})
metric = await metrics.emit('myspace/test', 'CoverageMetric', 1, [], {buffer: {count: 1}})
expect(metric).toBeDefined()
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
Expand Down
3 changes: 3 additions & 0 deletions test/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ test('Alloc', async () => {
expect(inst).toEqual(metrics)
cache = CustomMetrics.getCache()
expect(Object.keys(cache).length).toBe(1)
CustomMetrics.flushAll()

CustomMetrics.freeInstance(tags)
inst = CustomMetrics.getInstance(tags)
expect(inst).toBeUndefined()
expect(cache).toBeDefined()
expect(Object.keys(cache).length).toBe(0)

CustomMetrics.freeInstanceByKey('unknown')
})

test('Destroy Table', async () => {
Expand Down
77 changes: 75 additions & 2 deletions test/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ test('Constructor with OneTable', async () => {
})

test('Constructor with client', async () => {
let metrics = new CustomMetrics({client: Client, tableName: TableName, owner: 'service', log: false})
let metrics = new CustomMetrics({client: Client, tableName: TableName})
expect(metrics).toBeDefined()
expect(metrics instanceof CustomMetrics).toBe(true)
expect(typeof metrics.emit == 'function').toBe(true)
})

test('Constructor with custom spans', async () => {
const Spans = [{period: 86400, samples: 24}]
let metrics = new CustomMetrics({onetable: table, owner: 'service', log: false, spans: Spans})
let metrics = new CustomMetrics({onetable: table, spans: Spans})

let timestamp = new Date(2000, 0, 1).getTime()
let metric
Expand All @@ -72,6 +72,79 @@ test('Constructor with custom spans', async () => {
expect(r.points[0].count).toBe(1)
})

test('Constructor with options', async () => {
// Log true
let metrics = new CustomMetrics({onetable: table, log: true})
expect(metrics).toBeDefined()

// Verbose log
metrics = new CustomMetrics({onetable: table, log: 'verbose'})
expect(metrics).toBeDefined()

// Custom log
metrics = new CustomMetrics({onetable: table, log: {
info: (message: string, context: {}) => null,
error: (message: string, context: {}) => null,
}})
expect(metrics).toBeDefined()

// Pre-defined onetable models
table.addModel('Metric', Schema.models.Metric)
metrics = new CustomMetrics({onetable: table})
expect(metrics).toBeDefined()

// Verbose log
metrics = new CustomMetrics({onetable: table, log: 'verbose'})
expect(metrics).toBeDefined()

// DynamoDB prefix
metrics = new CustomMetrics({onetable: table, prefix: 'met'})
expect(metrics).toBeDefined()

// TTL
metrics = new CustomMetrics({onetable: table, ttl: 86400})
expect(metrics).toBeDefined()


expect(() => {
// empty spans
new CustomMetrics({onetable: table, spans: []})
}).toThrow()
expect(() => {
// Invalid pResolution
new CustomMetrics({onetable: table, pResolution: -1})
}).toThrow()
expect(() => {
// Invalid buffer
new CustomMetrics({onetable: table, buffer: true as any})
}).toThrow()
expect(() => {
// Bad TTL
new CustomMetrics({onetable: table, ttl: true as any})
}).toThrow()
expect(() => {
// Bad Source
new CustomMetrics({onetable: table, source: true as any})
}).toThrow()
expect(() => {
// Missing database
new CustomMetrics({})
}).toThrow()
expect(() => {
// Missing table name
new CustomMetrics({client: Client})
}).toThrow()
expect(() => {
// Missing options
new CustomMetrics()
}).toThrow()
})

test('Constructor coverage', async () => {
new CustomMetrics({onetable: table, buffer: {sum: 100}})
new CustomMetrics({onetable: table, source: 'internal'})
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
Expand Down
24 changes: 22 additions & 2 deletions test/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ test('Test basic emit', async () => {
expect(r.points[0].value).toBe(10)
expect(r.points[0].count).toBe(1)
expect(r.points[0].timestamp).toBe(timestamp)
// dump('RESULTS', list)

})

test('Test emit with dimensions', async () => {
Expand Down Expand Up @@ -93,6 +91,28 @@ test('Test emit with dimensions', async () => {
expect(r.points.length).toBe(0)
})

test('Emit API', async () => {
let metrics = new CustomMetrics({onetable: table})
expect(async () => {
await metrics.emit('myspace/test', 'Launches', null as any)
}).rejects.toThrow()
expect(async () => {
await metrics.emit('myspace/test', 'Launches', undefined as any)
}).rejects.toThrow()
expect(async () => {
await metrics.emit('myspace/test', 'Launches', 'invalid' as any)
}).rejects.toThrow()
expect(async () => {
await metrics.emit(null as any, 'Launches', 10)
}).rejects.toThrow()
expect(async () => {
await metrics.emit('namespace', null as any, 10)
}).rejects.toThrow()

// Emit with ttl
await metrics.emit('myspace/test', 'ShortLived', 10, [], {ttl: 3600})
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
Expand Down
67 changes: 67 additions & 0 deletions test/gap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
gap.ts - Test gaps in metrics
*/
import {Client, Schema, CustomMetrics, DefaultSpans, log, Table, dump} from './utils/init'

// jest.setTimeout(7200 * 1000)

const TableName = 'GapTestTable'
const table = new Table({
name: TableName,
client: Client,
partial: true,
senselogs: log,
schema: Schema,
})

test('Create Table', async () => {
// This will create a local table
if (!(await table.exists())) {
await table.createTable()
expect(await table.exists()).toBe(true)
}
})

test('Test gaps between emit and query', async () => {
let metrics = new CustomMetrics({onetable: table, owner: 'service', log: true})
let timestamp = new Date(2000, 0, 1).getTime()
let span = DefaultSpans[0]
let interval = span.period / span.samples

let metric
for (let i = 0; i < span.samples * 20; i++) {
metric = await metrics.emit('myspace/test', 'GapMetric', 10, [], {timestamp})
timestamp += interval * 1000
}
expect(metric.spans[2].points.length).toBe(1)

timestamp += 5 * 86400 * 1000
let r = await metrics.query('myspace/test', 'GapMetric', {}, 86400, 'sum', {timestamp})
expect(r.points.length).toBe(0)
})

test('Test data aging beyond highest span', async () => {
const Spans = [{period: 3600, samples: 4}]
let metrics = new CustomMetrics({onetable: table, owner: 'service2', log: false, spans: Spans})

let timestamp = new Date(2000, 0, 1).getTime()
let span = Spans[0]
let interval = span.period / span.samples

// Emit more data than will fit in the span
let metric
for (let i = 0; i < span.samples * 2; i++) {
metric = await metrics.emit('myspace/test', 'MyMetric', 1, [], {timestamp})
timestamp += interval * 1000
}
expect(metric.spans[0].points.length).toBe(4)

let r = await metrics.query('myspace/test', 'MyMetric', {}, 86400, 'sum', {timestamp, accumulate: true})
expect(r.points.length).toBe(1)
expect(r.points[0].value).toBe(4)
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
})
7 changes: 7 additions & 0 deletions test/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ test('Test', async () => {
expect(JSON.stringify(list.dimensions![1])).toBe('{"Rocket":"SaturnV"}')
})

test('List API', async () => {
// With logging to pass through to OneTable find
let metrics = new CustomMetrics({onetable: table})
let list = await metrics.getMetricList('myspace/test', 'Launches', {log: false})
expect(list).toBeDefined()
})

test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
Expand Down
5 changes: 5 additions & 0 deletions test/owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ test('Create Table', async () => {
}
})

test('Constructor no owner', async () => {
let metrics = new CustomMetrics({onetable: table})
expect(metrics).toBeDefined()
})

test('Constructor with different owners', async () => {
let m1 = new CustomMetrics({onetable: table, owner: 'app1', log: true})
let m2 = new CustomMetrics({onetable: table, owner: 'service2', log: true})
Expand Down
Loading

0 comments on commit 6d6e727

Please sign in to comment.