-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ocean.core.Verify
#214
Merged
gavin-norman-sociomantic
merged 2 commits into
sociomantic-tsunami:v2.x.x
from
mihails-strasuns-sociomantic:verify
Aug 21, 2017
Merged
ocean.core.Verify
#214
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
* `ocean.core.Verify` | ||
|
||
New module with a single function, `verify`, intended to serve as drop-in | ||
replacement for `assert` to comply to [Sociomantic assert/enforce | ||
policies](https://github.com/sociomantic-tsunami/sociomantic/blob/master/Code/assert-vs-enforce.rst) | ||
|
||
It works similar to `enforce` as it throws `Exception` instead of an `Error` | ||
and will remain even when built with `-release`. But it also uses specific | ||
pre-constructed `SanityException` type to indicate importance of the failure. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/******************************************************************************* | ||
|
||
Utility intended as a replacement for `assert` to check for programming | ||
errors and sanity violations in situations when neither removing the check | ||
in -release mode nor bringing down the application by throwing an `Error` | ||
is acceptable. | ||
|
||
This module must have as few import dependencies as possible so that it can | ||
be used in place of `assert` freely without introducing cyclic imports. | ||
|
||
Copyright: Copyright (c) 2017 sociomantic labs GmbH. All rights reserved | ||
|
||
License: | ||
Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. | ||
Alternatively, this file may be distributed under the terms of the Tango | ||
3-Clause BSD License (see LICENSE_BSD.txt for details). | ||
|
||
*******************************************************************************/ | ||
|
||
module ocean.core.Verify; | ||
|
||
import ocean.meta.types.Qualifiers : istring; | ||
|
||
/******************************************************************************* | ||
|
||
Verifies that certain condition is met. | ||
|
||
Params: | ||
ok = boolean condition to check | ||
msg = optional exception message | ||
|
||
Throws: | ||
SanityException if `ok` condition is `false`. | ||
|
||
*******************************************************************************/ | ||
|
||
public void verify ( bool ok, lazy istring msg = "", | ||
istring file = __FILE__, int line = __LINE__ ) | ||
{ | ||
static SanityException exc; | ||
|
||
if (exc is null) | ||
exc = new SanityException(""); | ||
|
||
if (!ok) | ||
{ | ||
exc.file = file; | ||
exc.line = line; | ||
exc.msg = msg; | ||
|
||
throw exc; | ||
} | ||
} | ||
|
||
unittest | ||
{ | ||
try | ||
{ | ||
verify(false); | ||
} | ||
catch (SanityException e) { } | ||
|
||
verify(true); | ||
} | ||
|
||
/******************************************************************************* | ||
|
||
Indicates some internal sanity violation in the app, essentially a less | ||
fatal version of `AssertError`. | ||
|
||
*******************************************************************************/ | ||
|
||
public class SanityException : Exception | ||
{ | ||
public this ( istring msg, istring file = __FILE__, int line = __LINE__ ) | ||
{ | ||
super(msg, file, line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import core.thread; | |
|
||
import ocean.transition; | ||
import ocean.core.Enforce; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is enforce still needed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, there are also regular exceptions in scheduler. |
||
import ocean.core.Verify; | ||
import ocean.core.Traits; | ||
import ocean.core.array.Mutation : reverse; | ||
import ocean.io.select.EpollSelectDispatcher; | ||
|
@@ -130,16 +131,6 @@ final class Scheduler | |
/// ditto | ||
private State state = State.Initial; | ||
|
||
/*************************************************************************** | ||
|
||
Thrown instead of `AssertError` when scheduler sanity is violated. Such | ||
issues are almost impossible to reason about if not caught in time | ||
thus we want all sanity checks to remain even in `-release` mode. | ||
|
||
***************************************************************************/ | ||
|
||
private SchedulerSanityException sanity_e; | ||
|
||
/*************************************************************************** | ||
|
||
Thrown when all fibers are busy, task queue is full and no custom | ||
|
@@ -278,12 +269,10 @@ final class Scheduler | |
config.suspended_task_limit | ||
); | ||
|
||
this.sanity_e = new SchedulerSanityException; | ||
this.queue_full_e = new TaskQueueFullException; | ||
this.suspend_queue_full_e = new SuspendQueueFullException; | ||
|
||
enforce( | ||
this.sanity_e, | ||
verify( | ||
config.task_queue_limit >= config.worker_fiber_limit, | ||
"Must configure task queue size at least equal to worker fiber " ~ | ||
"count for optimal task scheduler performance." | ||
|
@@ -569,7 +558,7 @@ final class Scheduler | |
as there is at least one event registered. | ||
|
||
Throws: | ||
SchedulerSanityException if there are some active worker fibers | ||
SanityException if there are some active worker fibers | ||
left in the pool by the time there are not events left | ||
|
||
***************************************************************************/ | ||
|
@@ -602,9 +591,9 @@ final class Scheduler | |
foreach (ref fiber; iterator) | ||
fiber.active_task.kill(); | ||
|
||
enforce(this.sanity_e, this.fiber_pool.num_busy() == 0); | ||
enforce(this.sanity_e, this.queued_tasks.length() == 0); | ||
enforce(this.sanity_e, this.suspended_tasks.length() == 0); | ||
verify(this.fiber_pool.num_busy() == 0); | ||
verify(this.queued_tasks.length() == 0); | ||
verify(this.suspended_tasks.length() == 0); | ||
} | ||
|
||
/*************************************************************************** | ||
|
@@ -645,7 +634,7 @@ final class Scheduler | |
task = task to run | ||
|
||
Throws: | ||
SchedulerSanityException on attempt to run new task from the very | ||
SanityException on attempt to run new task from the very | ||
same fiber which would result in fiber resetting own state. | ||
|
||
***************************************************************************/ | ||
|
@@ -751,7 +740,7 @@ final class Scheduler | |
for (auto i = 0; i < current_count; ++i) | ||
{ | ||
Task task; | ||
enforce(this.sanity_e, this.suspended_tasks.pop(task)); | ||
verify(this.suspended_tasks.pop(task)); | ||
if (this.state == State.Shutdown) | ||
task.kill(); | ||
else | ||
|
@@ -1011,21 +1000,6 @@ public class SuspendQueueFullException : Exception | |
} | ||
} | ||
|
||
/****************************************************************************** | ||
|
||
Exception class that indicates scheduler internal sanity violation, | ||
for example, worker fiber leak. | ||
|
||
******************************************************************************/ | ||
|
||
private class SchedulerSanityException : Exception | ||
{ | ||
this ( ) | ||
{ | ||
super("Internal sanity violation using the scheduler"); | ||
} | ||
} | ||
|
||
private void debug_trace ( T... ) ( cstring format, T args ) | ||
{ | ||
debug ( TaskScheduler ) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to start a bike shed war, but I wonder why you didn't go for
ProgrammingException
or if there was any discussion about the name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was no specific discussion, I picked the name because I was already using this name in other places to name things with such purpose. I believe it is a more generic and thus correct term than
ProgrammingException
because cases when we would want to use it may also detect architecture issues or serious violations from trusted input source (like DHT).