Module rodentia.test.process_test2

Expand source code
# -*- coding: utf-8 -*-
import unittest
import numpy as np
#from multiprocessing import Process, Pipe
from torch import multiprocessing as mp
import os, sys
sys.path.insert(0, os.getcwd())
import rodentia


COMMAND_ACTION    = 0
COMMAND_TERMINATE = 1

def worker(conn):
    env = rodentia.Environment(width=84, height=84,
                               bg_color=[0.0, 0.0, 0.0])

    # Add floor
    env.add_box(texture_path="",
                half_extent=[10.0, 1.0, 10.0],
                pos=[0.0, -1.0, 0.0],
                rot=0.0,
                detect_collision=False)

    # Add sphere
    sphere_id = env.add_sphere(texture_path="",
                               radius=1.0,
                               pos=[0.0, 2.0, -5.0],
                               rot=0.0,
                               detect_collision=True)
    conn.send(0)
  
    while True:
        command = conn.recv()

        if command == COMMAND_ACTION:
            action = np.array([10, 0, 0], dtype=np.int32)
            obs = env.step(action)
            conn.send(obs)
        elif command == COMMAND_TERMINATE:
            break
        else:
            print("bad command: {}".format(command))
  
    del env
    conn.send(0)
    conn.close()


class RodentiaProcessTest(unittest.TestCase):
  def testProcess(self):
      """
      env_dummy = rodentia.Environment(width=84, height=84,
                                       bg_color=[0.0, 0.0, 0.0])
      env_dummy.close()
      env_dummy = None
      """

      #use_context = False
      use_context = True

      conn0, child_conn0 = mp.Pipe()
      conn1, child_conn1 = mp.Pipe()

      if use_context:
          context = mp.get_context("fork")
          proc0 = context.Process(target=worker, args=(child_conn0,))
          proc1 = context.Process(target=worker, args=(child_conn1,))          
      else:
          proc0 = mp.Process(target=worker, args=(child_conn0,))
          proc1 = mp.Process(target=worker, args=(child_conn1,))
      
      proc0.start()
      proc1.start()
      
      conn0.recv()
      conn1.recv()
          
      conn0.send(COMMAND_ACTION)
      conn1.send(COMMAND_ACTION)
      
      obs0 = conn0.recv()
      obs1 = conn1.recv()

      screen0 = obs0["screen"]
      self.assertEqual( (84,84,3), screen0.shape )

      screen1 = obs1["screen"]
      self.assertEqual( (84,84,3), screen1.shape )
      
      conn0.send(COMMAND_TERMINATE)
      conn1.send(COMMAND_TERMINATE)



if __name__ == '__main__':
    unittest.main()

Functions

def worker(conn)
Expand source code
def worker(conn):
    env = rodentia.Environment(width=84, height=84,
                               bg_color=[0.0, 0.0, 0.0])

    # Add floor
    env.add_box(texture_path="",
                half_extent=[10.0, 1.0, 10.0],
                pos=[0.0, -1.0, 0.0],
                rot=0.0,
                detect_collision=False)

    # Add sphere
    sphere_id = env.add_sphere(texture_path="",
                               radius=1.0,
                               pos=[0.0, 2.0, -5.0],
                               rot=0.0,
                               detect_collision=True)
    conn.send(0)
  
    while True:
        command = conn.recv()

        if command == COMMAND_ACTION:
            action = np.array([10, 0, 0], dtype=np.int32)
            obs = env.step(action)
            conn.send(obs)
        elif command == COMMAND_TERMINATE:
            break
        else:
            print("bad command: {}".format(command))
  
    del env
    conn.send(0)
    conn.close()

Classes

class RodentiaProcessTest (methodName='runTest')

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the init method, the base class init method must always be called. It is important that subclasses should not change the signature of their init method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

Expand source code
class RodentiaProcessTest(unittest.TestCase):
  def testProcess(self):
      """
      env_dummy = rodentia.Environment(width=84, height=84,
                                       bg_color=[0.0, 0.0, 0.0])
      env_dummy.close()
      env_dummy = None
      """

      #use_context = False
      use_context = True

      conn0, child_conn0 = mp.Pipe()
      conn1, child_conn1 = mp.Pipe()

      if use_context:
          context = mp.get_context("fork")
          proc0 = context.Process(target=worker, args=(child_conn0,))
          proc1 = context.Process(target=worker, args=(child_conn1,))          
      else:
          proc0 = mp.Process(target=worker, args=(child_conn0,))
          proc1 = mp.Process(target=worker, args=(child_conn1,))
      
      proc0.start()
      proc1.start()
      
      conn0.recv()
      conn1.recv()
          
      conn0.send(COMMAND_ACTION)
      conn1.send(COMMAND_ACTION)
      
      obs0 = conn0.recv()
      obs1 = conn1.recv()

      screen0 = obs0["screen"]
      self.assertEqual( (84,84,3), screen0.shape )

      screen1 = obs1["screen"]
      self.assertEqual( (84,84,3), screen1.shape )
      
      conn0.send(COMMAND_TERMINATE)
      conn1.send(COMMAND_TERMINATE)

Ancestors

  • unittest.case.TestCase

Methods

def testProcess(self)

env_dummy = rodentia.Environment(width=84, height=84, bg_color=[0.0, 0.0, 0.0]) env_dummy.close() env_dummy = None

Expand source code
def testProcess(self):
    """
    env_dummy = rodentia.Environment(width=84, height=84,
                                     bg_color=[0.0, 0.0, 0.0])
    env_dummy.close()
    env_dummy = None
    """

    #use_context = False
    use_context = True

    conn0, child_conn0 = mp.Pipe()
    conn1, child_conn1 = mp.Pipe()

    if use_context:
        context = mp.get_context("fork")
        proc0 = context.Process(target=worker, args=(child_conn0,))
        proc1 = context.Process(target=worker, args=(child_conn1,))          
    else:
        proc0 = mp.Process(target=worker, args=(child_conn0,))
        proc1 = mp.Process(target=worker, args=(child_conn1,))
    
    proc0.start()
    proc1.start()
    
    conn0.recv()
    conn1.recv()
        
    conn0.send(COMMAND_ACTION)
    conn1.send(COMMAND_ACTION)
    
    obs0 = conn0.recv()
    obs1 = conn1.recv()

    screen0 = obs0["screen"]
    self.assertEqual( (84,84,3), screen0.shape )

    screen1 = obs1["screen"]
    self.assertEqual( (84,84,3), screen1.shape )
    
    conn0.send(COMMAND_TERMINATE)
    conn1.send(COMMAND_TERMINATE)