Robotics StackExchange | Archived questions

when I perform pytest on this script, the script does not run the service after printing waiting for service. The service blocks again and again. Need help regarding this. Thank you.

import rclpy

from rclpy.node import Node

from sample_interfaces.srv import AddTwoStrings

import random

unittest

import unittest

import pytest

class AddTwoStringTestClass(unittest.Testcase):

def setUp(self):
    rcply.init(self):
    self.node_name = "node_" + str(random.randint(100,10000))
    print(self.node_name)
    self.node = Node(self.node_name)
    self.client = self.node.create_client(AddTwoStrings, "add_two_strings")

def tearDown(self):
    self.node.destiny_client(self.client)
    self.node.destory_node()
    rclpy.shutdown()

def test_a_call_service(self):
    print("Starting test A")
    request = AddTwoStrings.Request()
    request.str1 = "Some String"
    request.str2 = "Second String"
    self.future = self.client.call_async(request)
    while rclpy.ok():
        print('inside the loop')
        rclpy.spin_one(self.node)
        if self.client.wait_for_service(timeout_sec = 1.0):
            print('waiting for service')
            #rclpy.spin_once(self.node)
            continue
        print(self.client.call(request))
        if self.future.done():
            try:
                response = self.future.result()
            except Exception as e:
                print('error arrived')
                assert 1==0
                break
            else:
                print("Result received:", response.sum)
                break

Asked by urvi2802 on 2023-07-10 02:36:17 UTC

Comments

Answers