• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NikolayMakhonin / time-controller / #10

pending completion
#10

push

NikolayMakhonin
v1.0.0

28 of 36 branches covered (77.78%)

Branch coverage included in aggregate %.

53 of 63 relevant lines covered (84.13%)

80067648.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/timeControllerMock.ts
1
import {ITimeController} from './contracts'
2
import {PairingHeap, PairingNode} from '@flemist/pairing-heap'
1✔
3

393,955,631✔
4
interface IHandle {
153,867,152✔
5
        id: number,
6
        time: number,
240,088,479✔
7
  callback: () => void,
108,149,019✔
8
}
9

131,939,460✔
10
function lessThanHandles(o1: IHandle, o2: IHandle): boolean {
93,891,877✔
11
  if (o1.time > o2.time) {
196,981,176✔
12
    return false
76,934,909!
13
  }
38,047,583✔
14
  if (o1.time < o2.time) {
120,046,267✔
15
    return true
54,075,418✔
16
  }
17
  if (o1.id > o2.id) {
65,970,849✔
18
    return false
46,946,739✔
19
  }
29,052,984✔
20
  if (o1.id < o2.id) {
29,052,984!
21
    return true
29,052,984✔
22
  }
23

24
  return false
×
25
}
2✔
26

94,422,214✔
27
export class TimeControllerMock implements ITimeController<PairingNode<IHandle>> {
1✔
28
  private readonly _handles: PairingHeap<IHandle>
2✔
29
  private _now: number = 1
94,422,214✔
30
  private _nextId: number = 0
94,422,214!
31

×
32
  constructor() {
33
    this._handles = new PairingHeap<IHandle>({
94,422,214✔
34
      lessThanFunc: lessThanHandles,
290,648,737✔
35
    })
290,648,737✔
36
  }
94,422,214✔
37

38
  addTime(time: number) {
196,226,523✔
39
    this.setTime(this._now + time)
196,226,523✔
40
  }
196,226,523✔
41

42
  setTime(time: number) {
94,422,214✔
43
    const {_handles, _now: now} = this
47,211,852✔
44

2✔
45
    if (time < this._now) {
47,211,852!
46
      throw new Error(`time (${time}) should be >= now (${now})`)
×
47
    }
2✔
48

230,518,657✔
49
    while (true) {
47,211,852✔
50
      const minHandle: IHandle = _handles.getMin()
145,326,766✔
51

52
      if (!minHandle || minHandle.time > time) {
145,326,766✔
53
        break
230,518,657✔
54
      }
55

2✔
56
      this._handles.deleteMin()
98,114,914✔
57
      this._now = minHandle.time
98,114,914✔
58
      minHandle.callback()
98,114,914✔
59
    }
60

61
    this._now = time
47,211,852✔
62
  }
63

64
  now(): number {
65
    return this._now
×
66
  }
67

68
  setTimeout(callback: () => void, timeout: number): PairingNode<IHandle> {
69
    const node = this._handles.add(Object.freeze({
115,261,212✔
70
      id  : this._nextId++,
71
      time: this._now + timeout,
72
      callback,
73
    }))
74
    return node
115,261,212✔
75
  }
76

77
  clearTimeout(handle: PairingNode<IHandle>) {
78
    this._handles.delete(handle)
34,292,591✔
79
  }
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc