option-validation.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import Piscina from '..';
  2. import { test } from 'tap';
  3. test('filename cannot be non-null/non-string', async ({ throws }) => {
  4. throws(() => new Piscina(({
  5. filename: 12
  6. }) as any), /options.filename must be a string or null/);
  7. });
  8. test('name cannot be non-null/non-string', async ({ throws }) => {
  9. throws(() => new Piscina(({
  10. name: 12
  11. }) as any), /options.name must be a string or null/);
  12. });
  13. test('minThreads must be non-negative integer', async ({ throws }) => {
  14. throws(() => new Piscina(({
  15. minThreads: -1
  16. }) as any), /options.minThreads must be a non-negative integer/);
  17. throws(() => new Piscina(({
  18. minThreads: 'string'
  19. }) as any), /options.minThreads must be a non-negative integer/);
  20. });
  21. test('maxThreads must be positive integer', async ({ throws }) => {
  22. throws(() => new Piscina(({
  23. maxThreads: -1
  24. }) as any), /options.maxThreads must be a positive integer/);
  25. throws(() => new Piscina(({
  26. maxThreads: 0
  27. }) as any), /options.maxThreads must be a positive integer/);
  28. throws(() => new Piscina(({
  29. maxThreads: 'string'
  30. }) as any), /options.maxThreads must be a positive integer/);
  31. });
  32. test('concurrentTasksPerWorker must be positive integer', async ({ throws }) => {
  33. throws(() => new Piscina(({
  34. concurrentTasksPerWorker: -1
  35. }) as any), /options.concurrentTasksPerWorker must be a positive integer/);
  36. throws(() => new Piscina(({
  37. concurrentTasksPerWorker: 0
  38. }) as any), /options.concurrentTasksPerWorker must be a positive integer/);
  39. throws(() => new Piscina(({
  40. concurrentTasksPerWorker: 'string'
  41. }) as any), /options.concurrentTasksPerWorker must be a positive integer/);
  42. });
  43. test('idleTimeout must be non-negative integer', async ({ throws }) => {
  44. throws(() => new Piscina(({
  45. idleTimeout: -1
  46. }) as any), /options.idleTimeout must be a non-negative integer/);
  47. throws(() => new Piscina(({
  48. idleTimeout: 'string'
  49. }) as any), /options.idleTimeout must be a non-negative integer/);
  50. });
  51. test('maxQueue must be non-negative integer', async ({ throws, equal }) => {
  52. throws(() => new Piscina(({
  53. maxQueue: -1
  54. }) as any), /options.maxQueue must be a non-negative integer/);
  55. throws(() => new Piscina(({
  56. maxQueue: 'string'
  57. }) as any), /options.maxQueue must be a non-negative integer/);
  58. const p = new Piscina({ maxQueue: 'auto', maxThreads: 2 });
  59. equal(p.options.maxQueue, 4);
  60. });
  61. test('useAtomics must be a boolean', async ({ throws }) => {
  62. throws(() => new Piscina(({
  63. useAtomics: -1
  64. }) as any), /options.useAtomics must be a boolean/);
  65. throws(() => new Piscina(({
  66. useAtomics: 'string'
  67. }) as any), /options.useAtomics must be a boolean/);
  68. });
  69. test('resourceLimits must be an object', async ({ throws }) => {
  70. throws(() => new Piscina(({
  71. resourceLimits: 0
  72. }) as any), /options.resourceLimits must be an object/);
  73. });
  74. test('taskQueue must be a TaskQueue object', async ({ throws }) => {
  75. throws(() => new Piscina(({
  76. taskQueue: 0
  77. }) as any), /options.taskQueue must be a TaskQueue object/);
  78. throws(() => new Piscina(({
  79. taskQueue: 'test'
  80. }) as any), /options.taskQueue must be a TaskQueue object/);
  81. throws(() => new Piscina(({
  82. taskQueue: null
  83. }) as any), /options.taskQueue must be a TaskQueue object/);
  84. throws(() => new Piscina(({
  85. taskQueue: new Date()
  86. }) as any), /options.taskQueue must be a TaskQueue object/);
  87. throws(() => new Piscina(({
  88. taskQueue: { } as any
  89. }) as any), /options.taskQueue must be a TaskQueue object/);
  90. });
  91. test('niceIncrement must be non-negative integer on Unix', {
  92. skip: process.platform === 'win32' ? 'Unix options validate' : false
  93. }, async ({ throws }) => {
  94. throws(() => new Piscina(({
  95. niceIncrement: -1
  96. }) as any), /options.niceIncrement must be a non-negative integer/);
  97. throws(() => new Piscina(({
  98. niceIncrement: 'string'
  99. }) as any), /options.niceIncrement must be a non-negative integer/);
  100. });
  101. test('trackUnmanagedFds must be a boolean', async ({ throws }) => {
  102. throws(() => new Piscina(({
  103. trackUnmanagedFds: -1
  104. }) as any), /options.trackUnmanagedFds must be a boolean/);
  105. throws(() => new Piscina(({
  106. trackUnmanagedFds: 'string'
  107. }) as any), /options.trackUnmanagedFds must be a boolean/);
  108. });