Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.3 KiB

  1. global.TEST = true
  2. var Bottleneck = require('./bottleneck')
  3. var assert = require('assert')
  4. module.exports = function (options={}) {
  5. var mustEqual = function (a, b) {
  6. var strA = JSON.stringify(a)
  7. var strB = JSON.stringify(b)
  8. if (strA !== strB) {
  9. console.log(strA + ' !== ' + strB, (new Error('').stack))
  10. assert(strA === strB)
  11. }
  12. }
  13. var start
  14. var calls = []
  15. // set options.datastore
  16. if (options.datastore == null && process.env.DATASTORE === 'redis') {
  17. options.datastore = 'redis'
  18. options.clearDatastore = true
  19. } else if (options.datastore == null && process.env.DATASTORE === 'ioredis') {
  20. options.datastore = 'ioredis'
  21. options.clearDatastore = true
  22. } else {
  23. options.datastore = 'local'
  24. }
  25. var limiter = new Bottleneck(options)
  26. // limiter.on("debug", function (str, args) { console.log(`${Date.now()-start} ${str} ${JSON.stringify(args)}`) })
  27. if (!options.errorEventsExpected) {
  28. limiter.on("error", function (err) {
  29. console.log('(CONTEXT) ERROR EVENT', err)
  30. })
  31. }
  32. limiter.ready().then(function (client) {
  33. start = Date.now()
  34. })
  35. var getResults = function () {
  36. return {
  37. elapsed: Date.now() - start,
  38. callsDuration: calls.length > 0 ? calls[calls.length - 1].time : null,
  39. calls: calls
  40. }
  41. }
  42. var context = {
  43. job: function (err, ...result) {
  44. var cb = result.pop()
  45. calls.push({err: err, result: result, time: Date.now()-start})
  46. if (process.env.DEBUG) console.log(result, calls)
  47. cb.apply({}, [err].concat(result))
  48. },
  49. slowJob: function (duration, err, ...result) {
  50. setTimeout(function () {
  51. var cb = result.pop()
  52. calls.push({err: err, result: result, time: Date.now()-start})
  53. if (process.env.DEBUG) console.log(result, calls)
  54. cb.apply({}, [err].concat(result))
  55. }, duration)
  56. },
  57. promise: function (err, ...result) {
  58. return new Promise(function (resolve, reject) {
  59. if (process.env.DEBUG) console.log('In c.promise. Result: ', result)
  60. calls.push({err: err, result: result, time: Date.now()-start})
  61. if (process.env.DEBUG) console.log(result, calls)
  62. if (err == null) {
  63. return resolve(result)
  64. } else {
  65. return reject(err)
  66. }
  67. })
  68. },
  69. slowPromise: function (duration, err, ...result) {
  70. return new Promise(function (resolve, reject) {
  71. setTimeout(function () {
  72. if (process.env.DEBUG) console.log('In c.slowPromise. Result: ', result)
  73. calls.push({err: err, result: result, time: Date.now()-start})
  74. if (process.env.DEBUG) console.log(result, calls)
  75. if (err == null) {
  76. return resolve(result)
  77. } else {
  78. return reject(err)
  79. }
  80. }, duration)
  81. })
  82. },
  83. pNoErrVal: function (promise, ...expected) {
  84. if (process.env.DEBUG) console.log('In c.pNoErrVal. Expected:', expected)
  85. return promise.then(function (actual) {
  86. mustEqual(actual, expected)
  87. })
  88. },
  89. noErrVal: function (...expected) {
  90. return function (err, ...actual) {
  91. mustEqual(err, null)
  92. mustEqual(actual, expected)
  93. }
  94. },
  95. last: function (options) {
  96. var opt = options != null ? options : {}
  97. return limiter.schedule(opt, function () { return Promise.resolve(getResults()) })
  98. .catch(function (err) { console.error("Error in context.last:", err)})
  99. },
  100. wait: function (wait) {
  101. return new Promise(function (resolve, reject) {
  102. setTimeout(resolve, wait)
  103. })
  104. },
  105. limiter: limiter,
  106. mustEqual: mustEqual,
  107. mustExist: function (a) { assert(a != null) },
  108. results: getResults,
  109. checkResultsOrder: function (order) {
  110. mustEqual(order.length, calls.length)
  111. for (var i = 0; i < Math.max(calls.length, order.length); i++) {
  112. mustEqual(order[i], calls[i].result)
  113. }
  114. },
  115. checkDuration: function (shouldBe, minBound = 10) {
  116. var results = getResults()
  117. var min = shouldBe - minBound
  118. var max = shouldBe + 50
  119. if (!(results.callsDuration > min && results.callsDuration < max)) {
  120. console.error('Duration not around ' + shouldBe + '. Was ' + results.callsDuration)
  121. }
  122. assert(results.callsDuration > min && results.callsDuration < max)
  123. }
  124. }
  125. return context
  126. }