|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <gtest/gtest.h> |
| 7 | +#include <aws/testing/AwsTestHelpers.h> |
| 8 | +#include <aws/testing/MemoryTesting.h> |
| 9 | +#include <algorithm> |
| 10 | +#include <thread> |
| 11 | + |
| 12 | +#include <aws/bedrock-runtime/BedrockRuntimeClient.h> |
| 13 | +#include <aws/bedrock-runtime/BedrockRuntimeErrors.h> |
| 14 | +#include <aws/bedrock-runtime/model/ConverseStreamRequest.h> |
| 15 | +#include <aws/bedrock-runtime/model/ConverseStreamHandler.h> |
| 16 | +#include <aws/core/client/CoreErrors.h> |
| 17 | +#include <aws/core/utils/json/JsonSerializer.h> |
| 18 | +#include <aws/core/utils/Outcome.h> |
| 19 | +#include <aws/testing/TestingEnvironment.h> |
| 20 | +#include <aws/core/platform/Environment.h> |
| 21 | +#include <condition_variable> |
| 22 | +#include <chrono> |
| 23 | + |
| 24 | +using namespace Aws::BedrockRuntime; |
| 25 | +using namespace Aws::BedrockRuntime::Model; |
| 26 | +using namespace Aws::Client; |
| 27 | +using namespace Aws::Region; |
| 28 | + |
| 29 | + |
| 30 | +namespace |
| 31 | +{ |
| 32 | +static const char* ALLOCATION_TAG = "BedrockRuntimeTests"; |
| 33 | + |
| 34 | +class BedrockRuntimeTests : public ::testing::Test |
| 35 | +{ |
| 36 | +protected: |
| 37 | + std::shared_ptr<BedrockRuntimeClient> m_client; |
| 38 | + |
| 39 | + void SetUp() |
| 40 | + { |
| 41 | + Aws::Client::ClientConfiguration config; |
| 42 | + config.region = AWS_TEST_REGION; |
| 43 | + m_client = Aws::MakeShared<BedrockRuntimeClient>(ALLOCATION_TAG, config); |
| 44 | + } |
| 45 | + |
| 46 | + void TearDown() |
| 47 | + { |
| 48 | + |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +TEST_F(BedrockRuntimeTests, TestStreaming) |
| 53 | +{ |
| 54 | + std::shared_ptr<Aws::BedrockRuntime::Model::ConverseStreamHandler> streamHandler = Aws::MakeShared<Aws::BedrockRuntime::Model::ConverseStreamHandler>(ALLOCATION_TAG); |
| 55 | + |
| 56 | + Aws::BedrockRuntime::Model::ConverseStreamRequest bedrockRequest; |
| 57 | + // other request setup |
| 58 | + std::mutex mutex; |
| 59 | + std::condition_variable cv; |
| 60 | + auto startTime = std::chrono::system_clock::now(); |
| 61 | + bool responseReceived = false; |
| 62 | + streamHandler->SetInitialResponseCallbackEx([&](const Aws::BedrockRuntime::Model::ConverseStreamInitialResponse& , const Aws::Utils::Event::InitialResponseType awsResponseType) |
| 63 | + { |
| 64 | + std::unique_lock<std::mutex> lock(mutex); |
| 65 | + if (awsResponseType == Aws::Utils::Event::InitialResponseType::ON_RESPONSE) { |
| 66 | + responseReceived = true; |
| 67 | + cv.notify_one(); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + bedrockRequest.SetEventStreamHandler(*streamHandler); |
| 72 | + bedrockRequest.SetModelId("dummy model"); |
| 73 | + bedrockRequest.SetMessages({}); |
| 74 | + Aws::BedrockRuntime::Model::ConverseStreamOutcome outcome = m_client->ConverseStream(bedrockRequest); |
| 75 | + ASSERT_FALSE(outcome.IsSuccess()); |
| 76 | + std::unique_lock<std::mutex> lock(mutex); |
| 77 | + cv.wait_until(lock, startTime + std::chrono::seconds(10), [&] { |
| 78 | + return responseReceived; }); |
| 79 | + ASSERT_TRUE(responseReceived); |
| 80 | +} |
| 81 | +} |
0 commit comments