Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 291468

Allow kernel tests to fail or expect logged errors

$
0
0

Problem/Motivation

Sometimes, a system will log an error and carry on functioning, because that's the best thing to do in normal operation.

But in a test, the logged error is invisible, and we might want a test to either a) fail on an error, because something's gone wrong in the test, or b) expect the error, because we want to actually test the conditions that cause an error.

Examples of this include:

- The cron service catches exceptions thrown by hook_cron() implementations, logs them, and then continues as normal
- The queue runner catches exceptions thrown by queue workers, logs them, and then continues as normal

Proposed resolution

Add a way for tests to react to logged errors, in both Kernel tests and Functional tests.

It should be up to individual tests:

- whether to opt in to being aware of logs
- the level of log error above which the test will react

Suggestion #1 is:

- The test base class implements RfcLoggerTrait and registers itself as a logger
- The log() method throws an exception (of a dedicated class)
- Test cases may then use PHPUnit's expectException() to assert that an error is logged; otherwise, the test will fail

Suggestion #2 is:

Use Prophecy in some way to mock a logger.

Remaining tasks

Figure out how to handle various cases, and the various details of the implementation.

Update the patch.

User interface changes

None.

API changes

TBD.

Data model changes

None.

Original report by joachim

The cron service catches exceptions thrown by hook_cron() implementations:

      // Do not let an exception thrown by one module disturb another.
      try {
        $this->moduleHandler->invoke($module, 'cron');
      }
      catch (\Exception $e) {
        watchdog_exception('cron', $e);
        
        throw $e;
      }

and exceptions thrown by queue workers:

          catch (\Exception $e) {
            // In case of any other kind of exception, log it and leave the item
            // in the queue to be processed again later.
            watchdog_exception('cron', $e);
          }

This means that if you're testing either a hook_cron() implementation or a queue worker, your test output won't show any exceptions, and is thus harder to debug.

It would be useful to either always make these re-throw their exceptions during tests, or add a setting for this that test classes can set.


Viewing all articles
Browse latest Browse all 291468

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>