# -*- coding: utf-8 -*-
'''
Smoothtest
Copyright (c) 2014 Juju. Inc

Code Licensed under MIT License. See LICENSE file.
'''
import relative_import
import multiprocessing
import sys
from .Context import singleton_decorator
from .base import AutoTestBase
from .Master import Master


@singleton_decorator
class Main(AutoTestBase):
    def __init__(self, smoke=False):
        self._timeout = 1
        self.smoke = smoke
        self.ishell = None
        
    def run(self, test_config, embed_ipython=False, block=False):
        self.create_child(self.build_callback(test_config))
        if embed_ipython:
            s = self # nice alias
            self.embed()
            self.kill_child
            raise SystemExit(0)
        elif block:
            self.log.i(self._parent_conn.recv())
    
    def get_conn(self):
        return self._parent_conn
    
    def embed(self, **kwargs):
        """Call this to embed IPython at the current point in your program.
        """
        from IPython.terminal.ipapp import load_default_config
        from IPython.terminal.embed import InteractiveShellEmbed
        from .ipython_extension import load_extension
        config = kwargs.get('config')
        header = kwargs.pop('header', u'')
        compile_flags = kwargs.pop('compile_flags', None)
        if config is None:
            config = load_default_config()
            config.InteractiveShellEmbed = config.TerminalInteractiveShell
            kwargs['config'] = config
        self.ishell = InteractiveShellEmbed.instance(**kwargs)
        load_extension(self.ishell, self)
        self.ishell(header=header, stack_depth=2, compile_flags=compile_flags)
    
    @property
    def new_child(self):
        self._new_child()
    
    def send_test(self, **test_config):
        self.send_recv('new_test', **test_config)
        self.test_config = test_config

    def build_callback(self, test_config):
        self.test_config = test_config

        def child_callback(child_conn):
            master = Master(child_conn)
            poll = master.io_loop(**test_config)            
            while 1:
                poll.next()
        
        return child_callback
    
    def create_child(self, child_callback):
        parent_conn, child_conn = multiprocessing.Pipe()
        self._parent_conn = parent_conn
        
        def callback():
            self.log.i('Forking at %s.'%self.__class__.__name__)
            if self.ishell:
                self.ishell.exit_now = True
            sys.stdin.close()
            parent_conn.close()
            child_callback(child_conn)
        
        self.child_process = multiprocessing.Process(target=callback)
        self.child_process.start()
        child_conn.close()
    
    def poll(self):
        return self._parent_conn.poll()
    
    @property
    def test(self):
        answer = self.send_recv('parcial_callback')
        result, error = answer[0]
        if error:
            self.log.e(error)
        return result

    def send(self, cmd, *args, **kwargs):
        if self.poll():
            self.log.i('Remaining in buffer: %r'%self._parent_conn.recv())
        self._parent_conn.send(self.cmd(cmd, *args, **kwargs))
    
    def send_recv(self, cmd, *args, **kwargs):
        self.send(cmd, *args, **kwargs)
        return self._parent_conn.recv()
    
    @property
    def kill_child(self):
        if self._parent_conn and not self._parent_conn.closed: 
            self.log.i(self.send_recv(self._kill_command))
            self._parent_conn.close()
            self._parent_conn = None
        else:
            self.child_process.terminate()


def smoke_test_module():
    pass

if __name__ == "__main__":
    smoke_test_module()

@@ -178,49 +178,8 @@
 sys%0A
-from .Context import singleton_decorator%0A
 from
@@ -205,16 +205,16 @@
 estBase%0A
+
 from .Ma
@@ -238,29 +238,8 @@
 r%0A%0A%0A
-@singleton_decorator%0A
 clas
@@ -369,32 +369,62 @@
 f.ishell = None%0A
+        self.test_config = %7B%7D%0A
         %0A    def
@@ -474,32 +474,71 @@
 , block=False):%0A
+        self.test_config = test_config%0A
         self.cre
@@ -552,16 +552,17 @@
 ld(self.
+_
 build_ca
@@ -568,27 +568,16 @@
 allback(
-test_config
 ))%0A     
@@ -1720,19 +1720,67 @@
 elf.
-_new_child(
+kill_child%0A        self.create_child(self._build_callback()
 )%0A  
@@ -1920,16 +1920,17 @@
     def 
+_
 build_ca
@@ -1944,62 +1944,10 @@
 self
-, test_config):%0A        self.test_config = test_config
+):
 %0A%0A  
@@ -2058,18 +2058,21 @@
 io_loop(
-**
+self.
 test_con
@@ -3438,17 +3438,16 @@
 .closed:
- 
 %0A       
@@ -3447,35 +3447,33 @@
             
-self.log.i(
+answer = 
 self.send_re
@@ -3494,16 +3494,46 @@
 command)
+%0A            self.log.i(answer
 )%0A      
@@ -3593,32 +3593,58 @@
 ent_conn = None%0A
+            return answer%0A
         else:%0A  
@@ -3719,12 +3719,92 @@
     
-pass
+main = Main()%0A    main.run(%7B%7D, embed_ipython=False, block=False)%0A    main.kill_child
 %0A%0Aif

Add smoke test