Beispiel #1
0
    def testPool(self):
        pool = PythonProxyPool()
        runners = [Runner(range(0, 10000), pool) for _ in range(0, 3)]
        for runner in runners:
            runner.start()

        for runner in runners:
            runner.join()

        for runner in runners:
            self.assertTrue(runner.ok)
Beispiel #2
0
 def testRedirectToDeque(self):
     qout = deque()
     qerr = deque()
     self.gateway = JavaGateway.launch_gateway(
         redirect_stdout=qout, redirect_stderr=qerr)
     for i in range(10):
         self.gateway.jvm.System.out.println("Test")
         self.gateway.jvm.System.err.println("Test2")
     sleep()
     for i in range(10):
         self.assertEqual("Test\n", qout.pop())
         self.assertEqual("Test2\n", qerr.pop())
     self.assertEqual(0, len(qout))
     self.assertEqual(0, len(qerr))
Beispiel #3
0
 def testRedirectToQueue(self):
     qout = Queue()
     qerr = Queue()
     self.gateway = JavaGateway.launch_gateway(redirect_stdout=qout,
                                               redirect_stderr=qerr)
     for i in range(10):
         self.gateway.jvm.System.out.println("Test")
         self.gateway.jvm.System.err.println("Test2")
     sleep()
     for i in range(10):
         self.assertEqual("Test\n", qout.get())
         self.assertEqual("Test2\n", qerr.get())
     self.assertTrue(qout.empty)
     self.assertTrue(qerr.empty)
Beispiel #4
0
 def testRedirectToDeque(self):
     qout = deque()
     qerr = deque()
     self.gateway = JavaGateway.launch_gateway(redirect_stdout=qout,
                                               redirect_stderr=qerr)
     for i in range(10):
         self.gateway.jvm.System.out.println("Test")
         self.gateway.jvm.System.err.println("Test2")
     sleep()
     for i in range(10):
         self.assertEqual("Test\n", qout.pop())
         self.assertEqual("Test2\n", qerr.pop())
     self.assertEqual(0, len(qout))
     self.assertEqual(0, len(qerr))
Beispiel #5
0
 def testRedirectToQueue(self):
     qout = Queue()
     qerr = Queue()
     self.gateway = JavaGateway.launch_gateway(
         redirect_stdout=qout, redirect_stderr=qerr)
     for i in range(10):
         self.gateway.jvm.System.out.println("Test")
         self.gateway.jvm.System.err.println("Test2")
     sleep()
     for i in range(10):
         self.assertEqual("Test\n", qout.get())
         self.assertEqual("Test2\n", qerr.get())
     self.assertTrue(qout.empty)
     self.assertTrue(qerr.empty)
Beispiel #6
0
    def testRedirectToFile(self):
        (_, outpath) = tempfile.mkstemp(text=True)
        (_, errpath) = tempfile.mkstemp(text=True)

        stdout = open(outpath, "w")
        stderr = open(errpath, "w")

        try:
            self.gateway = JavaGateway.launch_gateway(
                redirect_stdout=stdout, redirect_stderr=stderr)
            for i in range(10):
                self.gateway.jvm.System.out.println("Test")
                self.gateway.jvm.System.err.println("Test2")
            # Should not be necessary
            quiet_close(stdout)
            quiet_close(stderr)

            # Test that the redirect files were written to correctly
            with open(outpath, "r") as stdout:
                lines = stdout.readlines()
                self.assertEqual(10, len(lines))
                self.assertEqual("Test\n", lines[0])

            with open(errpath, "r") as stderr:
                lines = stderr.readlines()
                self.assertEqual(10, len(lines))
                self.assertEqual("Test2\n", lines[0])
        finally:
            os.unlink(outpath)
            os.unlink(errpath)
Beispiel #7
0
    def testRedirectToFile(self):
        (_, outpath) = tempfile.mkstemp(text=True)
        (_, errpath) = tempfile.mkstemp(text=True)

        stdout = open(outpath, "w")
        stderr = open(errpath, "w")

        try:
            self.gateway = JavaGateway.launch_gateway(redirect_stdout=stdout,
                                                      redirect_stderr=stderr)
            for i in range(10):
                self.gateway.jvm.System.out.println("Test")
                self.gateway.jvm.System.err.println("Test2")
            # Should not be necessary
            quiet_close(stdout)
            quiet_close(stderr)

            # Test that the redirect files were written to correctly
            with open(outpath, "r") as stdout:
                lines = stdout.readlines()
                self.assertEqual(10, len(lines))
                self.assertEqual("Test\n", lines[0])

            with open(errpath, "r") as stderr:
                lines = stderr.readlines()
                self.assertEqual(10, len(lines))
                self.assertEqual("Test2\n", lines[0])
        finally:
            os.unlink(outpath)
            os.unlink(errpath)
Beispiel #8
0
    def testMultiProcessJVMAccess(self):
        workers = [TestJVM1(self.gateway) for _ in range(8)]

        for worker in workers:
            worker.start()

        for worker in workers:
            worker.join()
Beispiel #9
0
    def testMultiProcessSystemReference(self):
        System = self.gateway.jvm.java.lang.System
        workers = [TestJVM2(System) for _ in range(8)]

        for worker in workers:
            worker.start()

        for worker in workers:
            worker.join()
Beispiel #10
0
    def testMultiProcessJVMReference(self):
        jvm = self.gateway.jvm
        workers = [TestJVM3(jvm) for _ in range(8)]

        for worker in workers:
            worker.start()

        for worker in workers:
            worker.join()
Beispiel #11
0
 def testStress(self):
     # Real stress test!
     # runner1 = Runner(xrange(1,10000,2),self.gateway)
     # runner2 = Runner(xrange(1000,1000000,10000), self.gateway)
     # runner3 = Runner(xrange(1000,1000000,10000), self.gateway)
     # Small stress test
     runner1 = Runner(range(1, 10000, 1000), self.gateway)
     runner2 = Runner(range(1000, 1000000, 100000), self.gateway)
     runner3 = Runner(range(1000, 1000000, 100000), self.gateway)
     runner1.start()
     runner2.start()
     runner3.start()
     runner1.join()
     runner2.join()
     runner3.join()
     self.assertTrue(runner1.ok)
     self.assertTrue(runner2.ok)
     self.assertTrue(runner3.ok)
Beispiel #12
0
    def testStress(self):
        # Real stress test!
#        runner1 = Runner(xrange(1,10000,2),self.gateway)
#        runner2 = Runner(xrange(1000,1000000,10000), self.gateway)
#        runner3 = Runner(xrange(1000,1000000,10000), self.gateway)
        # Small stress test
        runner1 = Runner(range(1, 10000, 1000), self.gateway)
        runner2 = Runner(range(1000, 1000000, 100000), self.gateway)
        runner3 = Runner(range(1000, 1000000, 100000), self.gateway)
        runner1.start()
        runner2.start()
        runner3.start()
        runner1.join()
        runner2.join()
        runner3.join()
        self.assertTrue(runner1.ok)
        self.assertTrue(runner2.ok)
        self.assertTrue(runner3.ok)
 def testRedirectToDeque(self):
     end = os.linesep
     qout = deque()
     qerr = deque()
     self.gateway = JavaGateway.launch_gateway(redirect_stdout=qout,
                                               redirect_stderr=qerr)
     for i in range(10):
         self.gateway.jvm.System.out.println("Test")
         self.gateway.jvm.System.err.println("Test2")
     sleep()
     for i in range(10):
         self.assertEqual("Test{0}".format(end), qout.pop())
         # Assert IN because some Java/OS outputs some garbage on stderr.
         line = qerr.pop()
         if stderr_is_polluted(line):
             line = qerr.pop()
         self.assertEqual("Test2{0}".format(end), line)
     self.assertEqual(0, len(qout))
     self.assertEqual(0, len(qerr))
Beispiel #14
0
 def testRedirectToDeque(self):
     end = os.linesep
     qout = deque()
     qerr = deque()
     self.gateway = JavaGateway.launch_gateway(
         redirect_stdout=qout, redirect_stderr=qerr)
     for i in range(10):
         self.gateway.jvm.System.out.println("Test")
         self.gateway.jvm.System.err.println("Test2")
     sleep()
     for i in range(10):
         self.assertEqual("Test{0}".format(end), qout.pop())
         # Assert IN because some Java/OS outputs some garbage on stderr.
         line = qerr.pop()
         if stderr_is_polluted(line):
             line = qerr.pop()
         self.assertEqual("Test2{0}".format(end), line)
     self.assertEqual(0, len(qout))
     self.assertEqual(0, len(qerr))
Beispiel #15
0
    def close(self):
        """Closes all currently opened connections.

        This operation is not thread safe and is only a best effort strategy
        to close active connections.

        All connections are guaranteed to be closed only if no other thread
        is accessing the client and no call is pending.
        """
        size = len(self.deque)
        for _ in range(0, size):
            try:
                connection = self.deque.pop()
                quiet_close(connection)
            except Exception:
                pass
Beispiel #16
0
    def close(self):
        """Closes all currently opened connections.

        This operation is not thread safe and is only a best effort strategy
        to close active connections.

        All connections are guaranteed to be closed only if no other thread
        is accessing the client and no call is pending.
        """
        size = len(self.deque)
        for _ in range(0, size):
            try:
                connection = deque.pop()
                connection.close()
            except Exception:
                pass
Beispiel #17
0
    def testRedirectToFile(self):
        end = os.linesep
        (out_handle, outpath) = tempfile.mkstemp(text=True)
        (err_handle, errpath) = tempfile.mkstemp(text=True)

        stdout = open(outpath, "w")
        stderr = open(errpath, "w")

        try:
            self.gateway = JavaGateway.launch_gateway(
                redirect_stdout=stdout, redirect_stderr=stderr)
            for i in range(10):
                self.gateway.jvm.System.out.println("Test")
                self.gateway.jvm.System.err.println("Test2")
            self.gateway.shutdown()
            sleep()
            # Should not be necessary
            quiet_close(stdout)
            quiet_close(stderr)

            # Test that the redirect files were written to correctly
            with open(outpath, "r") as stdout:
                lines = stdout.readlines()
                self.assertEqual(10, len(lines))
                self.assertEqual("Test{0}".format(end), lines[0])

            with open(errpath, "r") as stderr:
                lines = stderr.readlines()
                if not stderr_is_polluted(lines[0]):
                    self.assertEqual(10, len(lines))
                    # XXX Apparently, it's \n by default even on windows...
                    # Go figure
                    self.assertEqual("Test2\n", lines[0])
        finally:
            os.close(out_handle)
            os.close(err_handle)
            os.unlink(outpath)
            os.unlink(errpath)
    def testRedirectToFile(self):
        end = os.linesep
        (out_handle, outpath) = tempfile.mkstemp(text=True)
        (err_handle, errpath) = tempfile.mkstemp(text=True)

        stdout = open(outpath, "w")
        stderr = open(errpath, "w")

        try:
            self.gateway = JavaGateway.launch_gateway(redirect_stdout=stdout,
                                                      redirect_stderr=stderr)
            for i in range(10):
                self.gateway.jvm.System.out.println("Test")
                self.gateway.jvm.System.err.println("Test2")
            self.gateway.shutdown()
            sleep()
            # Should not be necessary
            quiet_close(stdout)
            quiet_close(stderr)

            # Test that the redirect files were written to correctly
            with open(outpath, "r") as stdout:
                lines = stdout.readlines()
                self.assertEqual(10, len(lines))
                self.assertEqual("Test{0}".format(end), lines[0])

            with open(errpath, "r") as stderr:
                lines = stderr.readlines()
                if not stderr_is_polluted(lines[0]):
                    self.assertEqual(10, len(lines))
                    # XXX Apparently, it's \n by default even on windows...
                    # Go figure
                    self.assertEqual("Test2\n", lines[0])
        finally:
            os.close(out_handle)
            os.close(err_handle)
            os.unlink(outpath)
            os.unlink(errpath)
Beispiel #19
0
    def disconnect(self, receiver, sender=None, unique_id=None):
        """Unregisters a receiver for this signal.

        :param receiver: The callable that was registered to receive the
            signal.
        :param unique_id: The unique id of the callable if it was provided.
            Optional.
        :return: True if the receiver was found and disconnected. False
            otherwise.
        :rtype: bool
        """
        full_id = self._get_id(receiver, unique_id, sender)
        disconnected = False

        with self.lock:
            for index in range(len(self.receivers)):
                temp_id = self.receivers[index][0]
                if temp_id == full_id:
                    del self.receivers[index]
                    disconnected = True
                    break

        return disconnected
Beispiel #20
0
 def run(self):
     for i in range(3):
         print(self.gateway.jvm.java.lang.System.currentTimeMillis())
         sleep()
Beispiel #21
0
 def testLargeByteArray(self):
     # Regression test for #109, an error when passing large byte arrays.
     self.gateway.jvm.java.nio.ByteBuffer.wrap(bytearray(range(255)))
 def run(self):
     for i in range(3):
         print(self.gateway.jvm.java.lang.System.currentTimeMillis())
         sleep()
Beispiel #23
0
 def testRedirectToNullOtherProcessGroup(self):
     self.gateway = JavaGateway.launch_gateway(
         create_new_process_group=True)
     for i in range(4097):  # Hangs if not properly redirected
         self.gateway.jvm.System.out.println("Test")
Beispiel #24
0
 def testRedirectToNull(self):
     self.gateway = JavaGateway.launch_gateway()
     for i in range(4097):  # Hangs if not properly redirected
         self.gateway.jvm.System.out.println("Test")
 def run(self):
     for i in range(3):
         print(self.System.currentTimeMillis())
         sleep()
Beispiel #26
0
 def testLargeByteArray(self):
     # Regression test for #109, an error when passing large byte arrays.
     self.gateway.jvm.java.nio.ByteBuffer.wrap(bytearray(range(255)))
Beispiel #27
0
 def testRedirectToNullOtherProcessGroup(self):
     self.gateway = JavaGateway.launch_gateway(
         create_new_process_group=True)
     for i in range(4097):  # Hangs if not properly redirected
         self.gateway.jvm.System.out.println("Test")
Beispiel #28
0
 def testRedirectToNull(self):
     self.gateway = JavaGateway.launch_gateway()
     for i in range(4097):  # Hangs if not properly redirected
         self.gateway.jvm.System.out.println("Test")
Beispiel #29
0
 def run(self):
     for i in range(3):
         print(self.jvm.java.lang.System.currentTimeMillis())
         time.sleep(0.5)
Beispiel #30
0
 def run(self):
     for i in range(3):
         print(self.System.currentTimeMillis())
         sleep()