Skip to content

Commit 1c8b6a9

Browse files
JrGoodlejhheider
authored andcommitted
[wip] Add test
1 parent 2a9e9a8 commit 1c8b6a9

File tree

3 files changed

+212
-40
lines changed

3 files changed

+212
-40
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <iostream>
18+
19+
#include <folly/portability/GFlags.h>
20+
21+
#include <folly/init/Init.h>
22+
#include <wangle/bootstrap/ClientBootstrap.h>
23+
#include <wangle/channel/AsyncSocketHandler.h>
24+
#include <wangle/channel/EventBaseHandler.h>
25+
#include <wangle/codec/LineBasedFrameDecoder.h>
26+
#include <wangle/codec/StringCodec.h>
27+
28+
using namespace folly;
29+
using namespace wangle;
30+
31+
DEFINE_int32(port, 8080, "echo server port");
32+
DEFINE_string(host, "::1", "echo server address");
33+
34+
typedef Pipeline<folly::IOBufQueue&, std::string> EchoPipeline;
35+
36+
// the handler for receiving messages back from the server
37+
class EchoHandler : public HandlerAdapter<std::string> {
38+
public:
39+
void read(Context*, std::string msg) override {
40+
std::cout << "received back: " << msg;
41+
}
42+
void readException(Context* ctx, exception_wrapper e) override {
43+
std::cout << exceptionStr(e) << std::endl;
44+
close(ctx);
45+
}
46+
void readEOF(Context* ctx) override {
47+
std::cout << "EOF received :(" << std::endl;
48+
close(ctx);
49+
}
50+
};
51+
52+
// chains the handlers together to define the response pipeline
53+
class EchoPipelineFactory : public PipelineFactory<EchoPipeline> {
54+
public:
55+
EchoPipeline::Ptr newPipeline(std::shared_ptr<AsyncTransport> sock) override {
56+
auto pipeline = EchoPipeline::create();
57+
pipeline->addBack(AsyncSocketHandler(sock));
58+
pipeline->addBack(
59+
EventBaseHandler()); // ensure we can write from any thread
60+
pipeline->addBack(LineBasedFrameDecoder(8192, false));
61+
pipeline->addBack(StringCodec());
62+
pipeline->addBack(EchoHandler());
63+
pipeline->finalize();
64+
return pipeline;
65+
}
66+
};
67+
68+
int main(int argc, char** argv) {
69+
folly::Init init(&argc, &argv);
70+
71+
ClientBootstrap<EchoPipeline> client;
72+
client.group(std::make_shared<folly::IOThreadPoolExecutor>(1));
73+
client.pipelineFactory(std::make_shared<EchoPipelineFactory>());
74+
auto pipeline = client.connect(SocketAddress(FLAGS_host, FLAGS_port)).get();
75+
76+
try {
77+
while (true) {
78+
std::string line;
79+
std::getline(std::cin, line);
80+
if (line == "") {
81+
break;
82+
}
83+
84+
pipeline->write(line + "\r\n").get();
85+
if (line == "bye") {
86+
pipeline->close();
87+
break;
88+
}
89+
}
90+
} catch (const std::exception& e) {
91+
std::cout << exceptionStr(e) << std::endl;
92+
}
93+
94+
return 0;
95+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <folly/portability/GFlags.h>
18+
19+
#include <folly/init/Init.h>
20+
#include <wangle/bootstrap/ServerBootstrap.h>
21+
#include <wangle/channel/AsyncSocketHandler.h>
22+
#include <wangle/codec/LineBasedFrameDecoder.h>
23+
#include <wangle/codec/StringCodec.h>
24+
25+
using namespace folly;
26+
using namespace wangle;
27+
28+
DEFINE_int32(port, 8080, "echo server port");
29+
30+
typedef Pipeline<IOBufQueue&, std::string> EchoPipeline;
31+
32+
// the main logic of our echo server; receives a string and writes it straight
33+
// back
34+
class EchoHandler : public HandlerAdapter<std::string> {
35+
public:
36+
void read(Context* ctx, std::string msg) override {
37+
std::cout << "handling " << msg << std::endl;
38+
write(ctx, msg + "\r\n");
39+
}
40+
};
41+
42+
// where we define the chain of handlers for each messeage received
43+
class EchoPipelineFactory : public PipelineFactory<EchoPipeline> {
44+
public:
45+
EchoPipeline::Ptr newPipeline(std::shared_ptr<AsyncTransport> sock) override {
46+
auto pipeline = EchoPipeline::create();
47+
pipeline->addBack(AsyncSocketHandler(sock));
48+
pipeline->addBack(LineBasedFrameDecoder(8192));
49+
pipeline->addBack(StringCodec());
50+
pipeline->addBack(EchoHandler());
51+
pipeline->finalize();
52+
return pipeline;
53+
}
54+
};
55+
56+
int main(int argc, char** argv) {
57+
folly::Init init(&argc, &argv);
58+
59+
ServerBootstrap<EchoPipeline> server;
60+
server.childPipeline(std::make_shared<EchoPipelineFactory>());
61+
server.bind(FLAGS_port);
62+
server.waitForStop();
63+
64+
return 0;
65+
}

projects/github.com/facebook/wangle/package.yml

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,58 @@ test:
5151
dependencies:
5252
tea.xyz/gx/cc: c99
5353
script: |
54-
# cxx_flags = %W[
55-
# -std=c++17
56-
# -I#{include}
57-
# -I#{Formula["openssl@1.1"].opt_include}
58-
# -L#{Formula["gflags"].opt_lib}
59-
# -L#{Formula["glog"].opt_lib}
60-
# -L#{Formula["folly"].opt_lib}
61-
# -L#{Formula["fizz"].opt_lib}
62-
# -L#{lib}
63-
# -lgflags
64-
# -lglog
65-
# -lfolly
66-
# -lfizz
67-
# -lwangle
68-
# ]
69-
# if OS.linux?
70-
# cxx_flags << "-L#{Formula["boost"].opt_lib}"
71-
# cxx_flags << "-lboost_context-mt"
72-
# cxx_flags << "-ldl"
73-
# cxx_flags << "-lpthread"
74-
# end
54+
c++ $CXXFLAGS 'EchoClient.cpp' -o EchoClient
55+
c++ $CXXFLAGS 'EchoServer.cpp' -o EchoServer
7556
76-
# system ENV.cxx, pkgshare/"EchoClient.cpp", *cxx_flags, "-o", "EchoClient"
77-
# system ENV.cxx, pkgshare/"EchoServer.cpp", *cxx_flags, "-o", "EchoServer"
57+
# Find a free port
58+
FREE_PORT=0
59+
while :; do
60+
FREE_PORT=$(shuf -i 49152-65535 -n 1)
61+
if ! (nc -z 127.0.0.1 ${FREE_PORT}) 2>/dev/null; then
62+
break
63+
fi
64+
done
7865
79-
# port = free_port
80-
# fork { exec testpath/"EchoServer", "-port", port.to_s }
81-
# sleep 10
66+
# Start the EchoServer
67+
./EchoServer -port "${FREE_PORT}" &
68+
SERVER_PID=$!
69+
sleep 10
8270
83-
# require "pty"
84-
# output = ""
85-
# PTY.spawn(testpath/"EchoClient", "-port", port.to_s) do |r, w, pid|
86-
# w.write "Hello from Homebrew!\nAnother test line.\n"
87-
# sleep 20
88-
# Process.kill "TERM", pid
89-
# begin
90-
# r.each_line { |line| output += line }
91-
# rescue Errno::EIO
92-
# # GNU/Linux raises EIO when read is done on closed pty
93-
# end
94-
# end
95-
# assert_match("Hello from Homebrew!", output)
96-
# assert_match("Another test line.", output)
71+
# Run the EchoClient and interact with the EchoServer
72+
OUTPUT_FILE=output.txt
73+
rm -f "${OUTPUT_FILE}"
74+
mkfifo "${OUTPUT_FILE}"
75+
cat > "${OUTPUT_FILE}" &
76+
CLIENT_BACKGROUND_PID=$!
77+
78+
# Send test lines to the EchoClient
79+
./EchoClient -port "${FREE_PORT}" > "${OUTPUT_FILE}" << EOF
80+
Hello from tea!
81+
Another test line.
82+
EOF
83+
84+
sleep 20
85+
86+
# Kill the EchoServer and EchoClient
87+
kill "${SERVER_PID}"
88+
kill "${CLIENT_BACKGROUND_PID}"
89+
90+
# Check the output for the test lines
91+
grep -q "Hello from Homebrew!" "${OUTPUT_FILE}"
92+
grep -q "Another test line." "${OUTPUT_FILE}"
93+
94+
# Cleanup
95+
rm -f "${OUTPUT_FILE}"
96+
env:
97+
CXXFLAGS:
98+
- -std=c++17
99+
- -lgflags
100+
- -lglog
101+
- -lfolly
102+
- -lfizz
103+
- -lwangle
104+
linux:
105+
CXXFLAGS:
106+
- -lboost_context-mt
107+
- -ldl
108+
- -lpthread

0 commit comments

Comments
 (0)