summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/batch_test.callbacks.inc
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-01-08 06:36:34 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-01-08 06:36:34 +0000
commit0dd161277046bab1ec994e8d756c4e99c717421e (patch)
tree5f33a4f472b6b5e10544924e5bf6cfc0348b8d08 /modules/simpletest/tests/batch_test.callbacks.inc
parente07b9d35a1f4dcb1678c4d3bb6482daaebea6350 (diff)
downloadbrdo-0dd161277046bab1ec994e8d756c4e99c717421e.tar.gz
brdo-0dd161277046bab1ec994e8d756c4e99c717421e.tar.bz2
#629794 by yched: Fix Scaling issues with batch API. (with tests)
Diffstat (limited to 'modules/simpletest/tests/batch_test.callbacks.inc')
-rw-r--r--modules/simpletest/tests/batch_test.callbacks.inc118
1 files changed, 118 insertions, 0 deletions
diff --git a/modules/simpletest/tests/batch_test.callbacks.inc b/modules/simpletest/tests/batch_test.callbacks.inc
new file mode 100644
index 000000000..5f24757ee
--- /dev/null
+++ b/modules/simpletest/tests/batch_test.callbacks.inc
@@ -0,0 +1,118 @@
+<?php
+
+// $Id$
+
+/**
+ * @file
+ * Batch callbacks for the Batch API tests.
+ */
+
+/**
+ * Simple batch operation.
+ */
+function _batch_test_callback_1($id, $sleep, &$context) {
+ // No-op, but ensure the batch take a couple iterations.
+ usleep($sleep);
+ // Track execution, and store some result for post-processing in the
+ // 'finished' callback.
+ batch_test_stack("op 1 id $id");
+ $context['results'][1][] = $id;
+}
+
+/**
+ * Multistep batch operation.
+ */
+function _batch_test_callback_2($start, $total, $sleep, &$context) {
+ // Initialize context with progress information.
+ if (!isset($context['sandbox']['current'])) {
+ $context['sandbox']['current'] = $start;
+ $context['sandbox']['count'] = 0;
+ }
+
+ // Process by groups of 5 (arbitrary value).
+ $limit = 5;
+ for ($i = 0; $i < $limit && $context['sandbox']['count'] < $total; $i++) {
+ // No-op, but ensure the batch take a couple iterations.
+ usleep($sleep);
+ // Track execution, and store some result for post-processing in the
+ // 'finished' callback.
+ $id = $context['sandbox']['current'] + $i;
+ batch_test_stack("op 2 id $id");
+ $context['results'][2][] = $id;
+
+ // Update progress information.
+ $context['sandbox']['count']++;
+ }
+ $context['sandbox']['current'] += $i;
+
+ // Inform batch engine about progress.
+ if ($context['sandbox']['count'] != $total) {
+ $context['finished'] = $context['sandbox']['count'] / $total;
+ }
+}
+
+/**
+ * Batch operation setting up its own batch.
+ */
+function _batch_test_nested_batch_callback() {
+ batch_test_stack('setting up batch 2');
+ batch_set(_batch_test_batch_2());
+}
+
+/**
+ * Common 'finished' callbacks for batches 1 to 4.
+ */
+function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
+ $messages = array("results for batch $batch_id");
+ if ($results) {
+ foreach ($results as $op => $op_results) {
+ $messages[] = 'op '. $op . ': processed ' . count($op_results) . ' elements';
+ }
+ }
+ else {
+ $messages[] = 'none';
+ }
+
+ if (!$success) {
+ // A fatal error occurred during the processing.
+ $error_operation = reset($operations);
+ $messages[] = t('An error occurred while processing @op with arguments:<br/>@args', array('@op' => $error_operation[0], '@args' => print_r($error_operation[1], TRUE)));
+ }
+
+ drupal_set_message(implode('<br />', $messages));
+}
+
+/**
+ * 'finished' callback for batch 0.
+ */
+function _batch_test_finished_0($success, $results, $operations) {
+ _batch_test_finished_helper(0, $success, $results, $operations);
+}
+
+/**
+ * 'finished' callback for batch 1.
+ */
+function _batch_test_finished_1($success, $results, $operations) {
+ _batch_test_finished_helper(1, $success, $results, $operations);
+}
+
+/**
+ * 'finished' callback for batch 2.
+ */
+function _batch_test_finished_2($success, $results, $operations) {
+ _batch_test_finished_helper(2, $success, $results, $operations);
+}
+
+/**
+ * 'finished' callback for batch 3.
+ */
+function _batch_test_finished_3($success, $results, $operations) {
+ _batch_test_finished_helper(3, $success, $results, $operations);
+}
+
+/**
+ * 'finished' callback for batch 4.
+ */
+function _batch_test_finished_4($success, $results, $operations) {
+ _batch_test_finished_helper(4, $success, $results, $operations);
+}