ephemeral.test.ts (1670B)
1 import { Ephemeral } from "#core/domain/ephemeral.ts"; 2 import { assert } from "$std/assert/assert.ts"; 3 import { assertEquals } from "$std/assert/assert_equals.ts"; 4 import { assertFalse } from "$std/assert/assert_false.ts"; 5 import { FakeTime } from "$std/testing/time.ts"; 6 import * as fc from "fast-check"; 7 8 Deno.test({ 9 name: "unit ephemeral shouldn't out of date before ttl", 10 fn() { 11 fc.assert(fc.property(fc.integer({ min: 10 }), (ttl) => { 12 const expire = Ephemeral.of("hello world", ttl); 13 assertFalse(expire.isExpired); 14 })); 15 }, 16 }); 17 18 Deno.test({ 19 name: "unit ephemeral get back value if not expired", 20 fn() { 21 fc.assert( 22 fc.property(fc.integer({ min: 10 }), fc.string(), (ttl, value) => { 23 const expire = Ephemeral.of(value, ttl); 24 assertEquals(expire.valueOf(), value); 25 }), 26 ); 27 }, 28 }); 29 30 Deno.test({ 31 name: "unit ephemeral should out of date after ttl", 32 fn() { 33 fc.assert(fc.property(fc.integer({ min: 10 }), (ttl) => { 34 const clock = new FakeTime(); 35 try { 36 const expire = Ephemeral.of("whatever", ttl); 37 clock.tick((ttl + 1) * 1000); 38 assert(expire.isExpired); 39 } finally { 40 clock.restore(); 41 } 42 })); 43 }, 44 }); 45 46 Deno.test({ 47 name: "unit ephemeral should out of date after ttl", 48 fn() { 49 fc.assert( 50 fc.property(fc.integer({ min: 10 }), fc.string(), (ttl, value) => { 51 const clock = new FakeTime(); 52 try { 53 const expire = Ephemeral.of(value, ttl); 54 clock.tick((ttl + 1) * 1000); 55 assertEquals(expire.valueOf(), null); 56 } finally { 57 clock.restore(); 58 } 59 }), 60 ); 61 }, 62 });