Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 6 | class ActionScheduler_QueueCleaner_Test extends ActionScheduler_UnitTestCase { |
||
| 7 | |||
| 8 | public function test_delete_old_actions() { |
||
| 9 | $store = ActionScheduler::store(); |
||
| 10 | $runner = new ActionScheduler_QueueRunner( $store ); |
||
| 11 | |||
| 12 | $random = md5(rand()); |
||
| 13 | $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
||
| 14 | |||
| 15 | $created_actions = array(); |
||
| 16 | for ( $i = 0 ; $i < 5 ; $i++ ) { |
||
| 17 | $action = new ActionScheduler_Action( $random, array($random), $schedule ); |
||
| 18 | $created_actions[] = $store->save_action( $action ); |
||
| 19 | } |
||
| 20 | |||
| 21 | $runner->run(); |
||
| 22 | |||
| 23 | add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
||
| 24 | $cleaner = new ActionScheduler_QueueCleaner( $store ); |
||
| 25 | $cleaner->delete_old_actions(); |
||
| 26 | remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
||
| 27 | |||
| 28 | foreach ( $created_actions as $action_id ) { |
||
| 29 | $action = $store->fetch_action($action_id); |
||
| 30 | $this->assertFalse($action->is_finished()); // it's a NullAction |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public function test_delete_canceled_actions() { |
||
| 35 | $store = ActionScheduler::store(); |
||
| 36 | |||
| 37 | $random = md5(rand()); |
||
| 38 | $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
||
| 39 | |||
| 40 | $created_actions = array(); |
||
| 41 | for ( $i = 0 ; $i < 5 ; $i++ ) { |
||
| 42 | $action = new ActionScheduler_Action( $random, array($random), $schedule ); |
||
| 43 | $action_id = $store->save_action( $action ); |
||
| 44 | $store->cancel_action( $action_id ); |
||
| 45 | $created_actions[] = $action_id; |
||
| 46 | } |
||
| 47 | |||
| 48 | // track the actions that are deleted |
||
| 49 | $mock_action = new MockAction(); |
||
| 50 | add_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10, 1 ); |
||
| 51 | add_filter( 'action_scheduler_retention_period', '__return_zero' ); // delete any finished job |
||
| 52 | |||
| 53 | $cleaner = new ActionScheduler_QueueCleaner( $store ); |
||
| 54 | $cleaner->delete_old_actions(); |
||
| 55 | |||
| 56 | remove_filter( 'action_scheduler_retention_period', '__return_zero' ); |
||
| 57 | remove_action( 'action_scheduler_deleted_action', array( $mock_action, 'action' ), 10 ); |
||
| 58 | |||
| 59 | $deleted_actions = array_map( 'reset', $mock_action->get_args() ); |
||
| 60 | $this->assertEqualSets( $created_actions, $deleted_actions ); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function test_do_not_delete_recent_actions() { |
||
| 64 | $store = ActionScheduler::store(); |
||
| 65 | $runner = new ActionScheduler_QueueRunner( $store ); |
||
| 66 | |||
| 67 | $random = md5(rand()); |
||
| 68 | $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
||
| 69 | |||
| 70 | $created_actions = array(); |
||
| 71 | for ( $i = 0 ; $i < 5 ; $i++ ) { |
||
| 72 | $action = new ActionScheduler_Action( $random, array($random), $schedule ); |
||
| 73 | $created_actions[] = $store->save_action( $action ); |
||
| 74 | } |
||
| 75 | |||
| 76 | $runner->run(); |
||
| 77 | |||
| 78 | $cleaner = new ActionScheduler_QueueCleaner( $store ); |
||
| 79 | $cleaner->delete_old_actions(); |
||
| 80 | |||
| 81 | foreach ( $created_actions as $action_id ) { |
||
| 82 | $action = $store->fetch_action($action_id); |
||
| 83 | $this->assertTrue($action->is_finished()); // It's a FinishedAction |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | public function test_reset_unrun_actions() { |
||
| 88 | $store = ActionScheduler::store(); |
||
| 89 | |||
| 90 | $random = md5(rand()); |
||
| 91 | $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
||
| 92 | |||
| 93 | $created_actions = array(); |
||
| 94 | for ( $i = 0 ; $i < 5 ; $i++ ) { |
||
| 95 | $action = new ActionScheduler_Action( $random, array($random), $schedule ); |
||
| 96 | $created_actions[] = $store->save_action( $action ); |
||
| 97 | } |
||
| 98 | |||
| 99 | $store->stake_claim(10); |
||
| 100 | |||
| 101 | // don't actually process the jobs, to simulate a request that timed out |
||
| 102 | |||
| 103 | add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
||
| 104 | $cleaner = new ActionScheduler_QueueCleaner( $store ); |
||
| 105 | $cleaner->reset_timeouts(); |
||
| 106 | |||
| 107 | remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
||
| 108 | |||
| 109 | $claim = $store->stake_claim(10); |
||
| 110 | $this->assertEqualSets($created_actions, $claim->get_actions()); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function test_do_not_reset_failed_action() { |
||
| 114 | $store = ActionScheduler::store(); |
||
| 115 | |||
| 116 | $random = md5(rand()); |
||
| 117 | $schedule = new ActionScheduler_SimpleSchedule(as_get_datetime_object('1 day ago')); |
||
| 118 | |||
| 119 | $created_actions = array(); |
||
| 120 | for ( $i = 0 ; $i < 5 ; $i++ ) { |
||
| 121 | $action = new ActionScheduler_Action( $random, array($random), $schedule ); |
||
| 122 | $created_actions[] = $store->save_action( $action ); |
||
| 123 | } |
||
| 124 | |||
| 125 | $claim = $store->stake_claim(10); |
||
| 126 | foreach ( $claim->get_actions() as $action_id ) { |
||
| 127 | // simulate the first action interrupted by an uncatchable fatal error |
||
| 128 | $store->log_execution( $action_id ); |
||
| 129 | break; |
||
| 130 | } |
||
| 131 | |||
| 132 | add_filter( 'action_scheduler_timeout_period', '__return_zero' ); // delete any finished job |
||
| 133 | $cleaner = new ActionScheduler_QueueCleaner( $store ); |
||
| 134 | $cleaner->reset_timeouts(); |
||
| 135 | remove_filter( 'action_scheduler_timeout_period', '__return_zero' ); |
||
| 136 | |||
| 137 | $new_claim = $store->stake_claim(10); |
||
| 138 | $this->assertCount( 4, $new_claim->get_actions() ); |
||
| 139 | |||
| 140 | add_filter( 'action_scheduler_failure_period', '__return_zero' ); |
||
| 141 | $cleaner->mark_failures(); |
||
| 142 | remove_filter( 'action_scheduler_failure_period', '__return_zero' ); |
||
| 143 | |||
| 144 | $failed = $store->query_actions(array('status' => ActionScheduler_Store::STATUS_FAILED)); |
||
| 145 | $this->assertEquals( $created_actions[0], $failed[0] ); |
||
| 146 | $this->assertCount( 1, $failed ); |
||
| 147 | |||
| 148 | |||
| 149 | } |
||
| 150 | } |
||
| 151 |