The reverse of init()

1,908 views
Skip to first unread message

Baron Schwartz

unread,
Nov 16, 2013, 3:11:00 PM11/16/13
to golang-nuts
Is there an idiomatic and/or nice way to do something like the opposite of init(), to run some code in package scope when a program finishes? The use case is tearing down things (mock servers, for example) that were set up for test cases. I can only think of hacky ways to do it.

minux

unread,
Nov 16, 2013, 3:26:51 PM11/16/13
to Baron Schwartz, golang-nuts
On Sat, Nov 16, 2013 at 3:11 PM, Baron Schwartz <ba...@xaprb.com> wrote:
Is there an idiomatic and/or nice way to do something like the opposite of init(), to run some code in package scope when a program finishes? The use case is tearing down things (mock servers, for example) that were set up for test cases. I can only think of hacky ways to do it.
you need to devise your own mechanisms for this (for example, cmd/go has one).

search atexit on this mailing list for prior discussions on this, it's pretty extensive.

Michael Speer

unread,
Nov 16, 2013, 4:04:48 PM11/16/13
to Baron Schwartz, golang-nuts
Instead of having a module with global state, encapsulate that state into a class that is a context manager. Then you will an __enter__ statement, and an __exit__ statement that fires regardless of exceptions . You can also bind the value that is returned from __enter__ as a controller or interacting class, if that is helpful.


with test_harness() as test_controller
  test_controller.start_server('whatever')
  test_controller.run_test('other-thing')

# with statement has cleaned up here having called __exit__ at end of the previous block


On Sat, Nov 16, 2013 at 3:11 PM, Baron Schwartz <ba...@xaprb.com> wrote:
Is there an idiomatic and/or nice way to do something like the opposite of init(), to run some code in package scope when a program finishes? The use case is tearing down things (mock servers, for example) that were set up for test cases. I can only think of hacky ways to do it.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Michael Speer

unread,
Nov 16, 2013, 4:09:51 PM11/16/13
to Baron Schwartz, golang-nuts
Sorry, misread which list I was on. :(

You could still do a similar trick in go, having the module expose a function that accepts your code to run as first class function that handles bringing things up and tearing them down.

// pseudocode, obviously
testmodule.Run( func ( controller ){ 
  controller.DoThings();
});

John Nagle

unread,
Nov 16, 2013, 7:47:13 PM11/16/13
to golan...@googlegroups.com
On 11/16/2013 1:09 PM, Michael Speer wrote:
> Sorry, misread which list I was on. :(

That had me looking at which Usenet group I was in and
wondering if the news reader had failed.

John Nagle

adon...@google.com

unread,
Nov 20, 2013, 3:16:42 PM11/20/13
to golan...@googlegroups.com
On Saturday, 16 November 2013 15:11:00 UTC-5, Baron Schwartz wrote:
Is there an idiomatic and/or nice way to do something like the opposite of init(), to run some code in package scope when a program finishes? The use case is tearing down things (mock servers, for example) that were set up for test cases. I can only think of hacky ways to do it.

For what it's worth, the general advice within Google for clean shutdown of long-running server processes is: don't do it.  Shutdown hooks tend to be poorly tested (and hard to test), and they may have to run after a panic which has corrupted the state of the program (e.g. taken a lock and forgotten to unlock it) so they are a source of deadlocks.  _exit(2) is your friend.

Depending on your OS there may be more reliable mechanisms for effecting a shutdown hook, such as forking a child process that waits for the death of its parent.
Reply all
Reply to author
Forward
0 new messages