git.delta.rocks / unique-network / refs/commits / 9496746f52e9

difftreelog

test canceling of scheduled operations

Daniel Shiposha2022-09-12parent: #fd7a252.patch.diff
in: master

1 file changed

modifiedtests/src/.outdated/scheduler.test.tsdiffbeforeafterboth
18import chaiAsPromised from 'chai-as-promised';18import chaiAsPromised from 'chai-as-promised';
19import {19import {
20 default as usingApi,20 default as usingApi,
21 executeTransaction,
21 submitTransactionAsync,22 submitTransactionAsync,
22 submitTransactionExpectFailAsync,23 submitTransactionExpectFailAsync,
23} from '../substrate/substrate-api';24} from '../substrate/substrate-api';
45 cancelScheduled,46 cancelScheduled,
46 requirePallets,47 requirePallets,
47 Pallets,48 Pallets,
49 getBlockNumber,
50 scheduleAt,
48} from '../deprecated-helpers/helpers';51} from '../deprecated-helpers/helpers';
49import {IKeyringPair, SignatureOptions} from '@polkadot/types/types';52import {IKeyringPair, SignatureOptions} from '@polkadot/types/types';
50import {RuntimeDispatchInfo} from '@polkadot/types/interfaces';53import {RuntimeDispatchInfo} from '@polkadot/types/interfaces';
73 });76 });
74}77}
75
76function makeSignOptions(api: ApiPromise): SignatureOptions {
77 return objectSpread(
78 {blockHash: api.genesisHash, genesisHash: api.genesisHash},
79 {runtimeVersion: api.runtimeVersion, signedExtensions: api.registry.signedExtensions},
80 );
81}
8278
83describe('Scheduling token and balance transfers', () => {79describe('Scheduling token and balance transfers', () => {
84 let alice: IKeyringPair;80 let alice: IKeyringPair;
293 });289 });
294 });290 });
295291
296 // FIXME What purpose of this test?292 // Check if we can cancel a scheduled periodic operation
293 // in the same block in which it is running
297 it.skip('Can schedule a scheduled operation of canceling the scheduled operation', async () => {294 it('Can cancel the periodic sheduled tx when the tx is running', async () => {
298 await usingApi(async api => {295 await usingApi(async api => {
296 const currentBlockNumber = await getBlockNumber(api);
297 const blocksBeforeExecution = 10;
298 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;
299
299 const scheduledId = await makeScheduledId();300 const scheduledId = await makeScheduledId();
300
301 const waitForBlocks = 2;301 const scheduledCancelId = await makeScheduledId();
302
302 const period = 3;303 const period = 5;
303 const repetitions = 2;304 const repetitions = 5;
305
306 const initTestVal = 0;
307 const incTestVal = initTestVal + 1;
308 const finalTestVal = initTestVal + 2;
309 await executeTransaction(
310 api,
311 alice,
312 api.tx.testUtils.setTestValue(initTestVal),
313 );
314
315 const incTx = api.tx.testUtils.incTestValue();
316 const cancelTx = api.tx.scheduler.cancelNamed(scheduledId);
304317
305 await expect(scheduleAfter(318 await expect(scheduleAt(
306 api, 319 api,
307 api.tx.scheduler.cancelNamed(scheduledId), 320 incTx,
308 alice, 321 alice,
309 waitForBlocks, 322 firstExecutionBlockNumber,
310 scheduledId, 323 scheduledId,
311 period, 324 period,
312 repetitions,325 repetitions,
313 )).to.not.be.rejected;326 )).to.not.be.rejected;
327
328 // Cancel the inc tx after 2 executions
329 // *in the same block* in which the second execution is scheduled
330 await expect(scheduleAt(
331 api,
332 cancelTx,
333 alice,
334 firstExecutionBlockNumber + period,
335 scheduledCancelId,
336 )).to.not.be.rejected;
314337
315 await waitNewBlocks(waitForBlocks);338 await waitNewBlocks(blocksBeforeExecution);
316339
317 // todo:scheduler debug line; doesn't work (and doesn't appear in events) when executed in the same block as the scheduled transaction340 // execution #0
318 await expect(submitTransactionAsync(alice, api.tx.scheduler.cancelNamed(scheduledId))).to.not.be.rejected;341 expect((await api.query.testUtils.testValue()).toNumber())
342 .to.be.equal(incTestVal);
319343
320 let schedulerEvents = 0;344 await waitNewBlocks(period);
345
346 // execution #1
347 expect((await api.query.testUtils.testValue()).toNumber())
348 .to.be.equal(finalTestVal);
349
321 for (let i = 0; i < period * repetitions; i++) {350 for (let i = 1; i < repetitions; i++) {
322 const events = await api.query.system.events();351 await waitNewBlocks(period);
323 schedulerEvents += await expect(checkForFailedSchedulerEvents(api, events)).to.not.be.rejected;352 expect((await api.query.testUtils.testValue()).toNumber())
324 await waitNewBlocks(1);353 .to.be.equal(finalTestVal);
325 }354 }
326 expect(schedulerEvents).to.be.equal(repetitions);
327 });355 });
328 });356 });
329357
330 after(async () => {358 it('A scheduled operation can cancel itself', async () => {
331 // todo:scheduler to avoid the failed results of the previous test interfering with the next, delete after the problem is fixed
332 await waitNewBlocks(6);359 await usingApi(async api => {
360 const scheduledId = await makeScheduledId();
361 const waitForBlocks = 8;
362 const period = 2;
363 const repetitions = 5;
364
365 const initTestVal = 0;
366 const maxTestVal = 2;
367
368 await executeTransaction(
369 api,
370 alice,
371 api.tx.testUtils.setTestValue(initTestVal),
372 );
373
374 const selfCancelingTx = api.tx.testUtils.selfCancelingInc(scheduledId, maxTestVal);
375
376 await expect(scheduleAfter(
377 api,
378 selfCancelingTx,
379 alice,
380 waitForBlocks,
381 scheduledId,
382 period,
383 repetitions,
384 )).to.not.be.rejected;
385
386 await waitNewBlocks(waitForBlocks + 1);
387
388 // execution #0
389 expect((await api.query.testUtils.testValue()).toNumber())
390 .to.be.equal(initTestVal + 1);
391
392 await waitNewBlocks(period);
393
394 // execution #1
395 expect((await api.query.testUtils.testValue()).toNumber())
396 .to.be.equal(initTestVal + 2);
397
398 await waitNewBlocks(period);
399
400 // <canceled>
401 expect((await api.query.testUtils.testValue()).toNumber())
402 .to.be.equal(initTestVal + 2);
403 });
333 });404 });
334});405});
335406