fix:Add multi-threaded scenario use cases

Signed-off-by: chenchong_666 <chenchong57@huawei.com>
This commit is contained in:
chenchong_666 2024-07-13 16:51:12 +08:00
parent 0355448be8
commit ad3fe4ca13
8 changed files with 54 additions and 1 deletions

View File

@ -55,6 +55,7 @@ public:
int TestUnRegisterRemoteStub(const char *descriptor) override;
sptr<IRemoteObject> TestQueryRemoteProxy(const char *descriptor) override;
int TestSendTooManyRequest(int data, int &reply) override;
int TestMultiThreadSendRequest(int data, int &reply) override;
private:
int testFd_;
std::mutex remoteObjectsMutex_;

View File

@ -46,6 +46,7 @@ public:
void TestRegisterRemoteStub();
void TestUnRegisterRemoteStub();
void TestSendTooManyRequest();
void TestMultiThreadSendRequest();
private:
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "TestServiceClient" };

View File

@ -31,5 +31,6 @@ enum TestCommand {
TEST_CMD_NATIVE_IPC_REGISTER_REMOTE_STUB_OBJECT = 12,
TEST_CMD_NATIVE_IPC_REQUESTS = 13,
TEST_CMD_TOO_MANY_SENDREQUEST = 14,
TEST_CMD_MULTI_THREAD_SEND = 15,
};
#endif // OHOS_IPC_TEST_SERVICE_COMMAND_H

View File

@ -85,6 +85,7 @@ public:
virtual int TestUnRegisterRemoteStub(const char *descriptor) = 0;
virtual sptr<IRemoteObject> TestQueryRemoteProxy(const char *descriptor) = 0;
virtual int TestSendTooManyRequest(int data, int &reply) = 0;
virtual int TestMultiThreadSendRequest(int data, int &reply) = 0;
public:
DECLARE_INTERFACE_DESCRIPTOR(u"test.ipc.ITestService");
@ -158,6 +159,7 @@ public:
int TestUnRegisterRemoteStub(const char *descriptor) override;
sptr<IRemoteObject> TestQueryRemoteProxy(const char *descriptor) override;
int TestSendTooManyRequest(int data, int &reply) override;
int TestMultiThreadSendRequest(int data, int &reply) override;
private:
static inline BrokerDelegator<TestServiceProxy> delegator_;

View File

@ -121,6 +121,9 @@ int main(int argc, char *argv[])
testClient->TestSendTooManyRequest();
testClient->StartSyncTransaction();
}},
{TestCommand::TEST_CMD_MULTI_THREAD_SEND, [&]() {
testClient->TestMultiThreadSendRequest();
}},
};
auto it = commandMap.find(commandId);

View File

@ -331,5 +331,10 @@ int TestService::TestSendTooManyRequest(int data, int &reply)
(void)reply;
return 0;
}
int TestService::TestMultiThreadSendRequest(int data, int &reply)
{
(void)data;
(void)reply;
return 0;
}
} // namespace OHOS

View File

@ -232,4 +232,14 @@ void TestServiceClient::TestSendTooManyRequest()
}
}
void TestServiceClient::TestMultiThreadSendRequest()
{
if (testService_ != nullptr) {
ZLOGD(LABEL, "TestMultiThreadSendRequest");
int ret = 0;
int value = 2024;
testService_->TestMultiThreadSendRequest(value, ret);
}
}
} // namespace OHOS

View File

@ -40,6 +40,7 @@ using namespace OHOS::HiviewDFX;
constexpr int32_t MAX_RECURSIVE_SENDS = 5;
constexpr int32_t SEDNREQUEST_TIMES = 50000;
constexpr int32_t NUMBER_OF_THREADS = 20;
void TestServiceStub::InitMessageProcessMap()
{
@ -132,6 +133,35 @@ int TestServiceProxy::TestSendTooManyRequest(int data, int &reply)
return error;
}
int TestServiceProxy::TestMultiThreadSendRequest(int data, int &reply)
{
int error = 0;
sptr<IRemoteObject> proxyObject = Remote();
for (int32_t i = 0; i < NUMBER_OF_THREADS; i++) {
// Different threads correspond to different sets of parcels.
std::thread t([&proxyObject, &data, &reply] {
MessageParcel dataParcel;
MessageParcel replyParcel;
MessageOption option;
if (data > 0) {
dataParcel.WriteInt32(data);
dataParcel.WriteInt32(0);
}
int error = proxyObject->SendRequest(TRANS_ID_SYNC_TRANSACTION, dataParcel, replyParcel, option);
if (error != 0) {
ZLOGD(LABEL, "SendRequest is failed: %{public}d", error);
return;
}
reply = replyParcel.ReadInt32();
});
t.detach();
std::cout << "Thead: " << i << std::endl;
}
return error;
}
int TestServiceProxy::TestAsyncTransaction(int data, int timeout)
{
MessageOption option = { MessageOption::TF_ASYNC };