Skip to content

Commit

Permalink
Merge pull request #23 from RikitoNoto/release/1.3.2
Browse files Browse the repository at this point in the history
Release/1.3.2
  • Loading branch information
RikitoNoto authored Jul 2, 2023
2 parents e163681 + e92cc6e commit 4a84eda
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ migrate_working_dir/
build/

.fvm/

/android/local.properties
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ made it possible to omit the command argument of the Message class.
change links in README.md to github repository, and change logo size.
### 1.3.1
change the folder name of exsample to example.
### 1.3.2
fix didn't call close method.
2 changes: 0 additions & 2 deletions android/local.properties

This file was deleted.

1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
/android/local.properties
11 changes: 7 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ class _NoCommandPage extends State<NoCommandPage> {
}
}

void _send() {
void _send() async {
final Camel<Socket, SocketConnectionPoint> sender =
Camel<Socket, SocketConnectionPoint>(Tcp());
// call send method for send message.
sender.send(
await sender.send(
SocketConnectionPoint(address: myAddress, port: port),
Message.fromBody(
command: "dummy",
body:
_sendText), // if you don't use command, a command arg is anything.
);
sender.close();
}

@override
Expand Down Expand Up @@ -155,17 +156,19 @@ class _CommandPage extends State<CommandPage> implements Command {
.listen(SocketConnectionPoint(address: myAddress, port: port))) {}
}

void _send() {
void _send() async{
final Camel<Socket, SocketConnectionPoint> sender =
Camel<Socket, SocketConnectionPoint>(Tcp());
// call send method for send message.
sender.send(
await sender.send(
SocketConnectionPoint(address: myAddress, port: port),
Message.fromBody(
command: "MY_COMMAND",
body:
_sendText), // if you don't use command, a command arg is anything.
);

sender.close();
}

@override
Expand Down
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "1.0.0"
characters:
dependency: transitive
description:
Expand Down Expand Up @@ -145,10 +145,10 @@ packages:
dependency: "direct dev"
description:
name: flutter_lints
sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
sha256: "2118df84ef0c3ca93f96123a616ae8540879991b8b57af2f81b76a7ada49b2a4"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "2.0.2"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
4 changes: 4 additions & 0 deletions lib/camel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ class Camel<T, C> {
yield data;
}
}

void close(){
communicator.close();
}
}
21 changes: 19 additions & 2 deletions lib/tcp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,26 @@ class CommunicateDataTcp {

class Tcp implements Communicator<Socket, SocketConnectionPoint> {
final Map<Socket, CommunicateDataTcp> _receiveBuffers = {};
final List<Socket> _connection = [];
final List<ServerSocket> _servers = [];

@override
Future<Socket> connect(SocketConnectionPoint connectionPoint) async {
return await _connect(connectionPoint.address, connectionPoint.port);
Socket connection = await _connect(connectionPoint.address, connectionPoint.port);
_connection.add(connection);
return connection;
}

@override
Future close() async {}
Future close() async {
for(Socket connection in _connection){
await connection.close();
}

for(ServerSocket server in _servers){
await server.close();
}
}

@override
Future<int> send(CommunicateData<Socket> data) async {
Expand All @@ -66,8 +78,10 @@ class Tcp implements Communicator<Socket, SocketConnectionPoint> {
final StreamController<CommunicateData<Socket>> controller =
StreamController();
ServerSocket serverSocket = await _bind(bind.address, bind.port);
_servers.add(serverSocket);
// connection
serverSocket.listen((Socket socket) {
_connection.add(socket);
// receive data
socket.listen((Uint8List data) {
// if have not received data yet.
Expand Down Expand Up @@ -110,6 +124,7 @@ class Tcp implements Communicator<Socket, SocketConnectionPoint> {
{dynamic sourceAddress,
int sourcePort,
Duration? timeout}) _connect = Socket.connect;

Function(
dynamic address,
int port, {
Expand All @@ -118,6 +133,7 @@ class Tcp implements Communicator<Socket, SocketConnectionPoint> {
bool shared,
}) _bind = ServerSocket.bind;

@visibleForTesting
void setConnectMock(
Function(dynamic, int,
{dynamic sourceAddress, int sourcePort, Duration? timeout})
Expand All @@ -127,6 +143,7 @@ class Tcp implements Communicator<Socket, SocketConnectionPoint> {
}
}

@visibleForTesting
void setBindMock(
Function(
dynamic address,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: camel
description: A Flutter package for communicating in a local network.
version: 1.3.1
version: 1.3.2
homepage:

environment:
Expand Down
108 changes: 80 additions & 28 deletions test/tcp_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ Future<ListenSpy> bindMock({
}) async {
Tcp tcp = Tcp();
ListenSpy spy = ListenSpy(
tcp: tcp,
socket: mockSocket ?? MockSocket(),
serverSocket: mockServerSocket ?? MockServerSocket());
tcp: tcp,
socket: mockSocket ?? MockSocket(),
serverSocket: mockServerSocket ?? MockServerSocket()
);

Future<ServerSocket> bindMock(
dynamic address,
Expand Down Expand Up @@ -122,6 +123,27 @@ void constructSocketMock(ListenSpy spy, List<String> receiveData) {
});
}

Future listen(ListenSpy spy, {bool waitReceive=false, Function(CommunicateData<Socket>)? callback, List<String>? receiveDatas}) async {
constructListenMock(spy, receiveDatas ?? []);
listenFunc() async {
await for (CommunicateData<Socket> data in await spy.tcp.listen(SocketConnectionPoint(address: "127.0.0.1", port: 1000))) {
if(callback == null){
break;
}
callback(data);
}
}

if(waitReceive){
await listenFunc();
}
else{
listenFunc();
await Future.delayed(const Duration(seconds: 0));
}
}


void useLibraryTest() {
group("connect", () {
test("should be hand over the IP address and the port number", () async {
Expand Down Expand Up @@ -157,31 +179,6 @@ void useLibraryTest() {
});
});

group("send", () {
test("should be write the empty message to the address handed over",
() async {
ConnectSpy spy = await connectWithMock(address: "127.0.0.1", port: 1000);
MockMessage mockMessage = MockMessage();
when(mockMessage.message).thenReturn("");
int sentByte = await spy.tcp
.send(CommunicateData(connection: spy.socket, message: mockMessage));

verify(spy.socket.write("0\n"));
expect(sentByte, 0);
});

test("should be write the message to the address handed over", () async {
ConnectSpy spy = await connectWithMock(address: "127.0.0.1", port: 1000);
MockMessage mockMessage = MockMessage();
when(mockMessage.message).thenReturn("message");
int sentByte = await spy.tcp
.send(CommunicateData(connection: spy.socket, message: mockMessage));

verify(spy.socket.write("7\nmessage"));
expect(sentByte, 7);
});
});

group("listen", () {
test("should be execute received message command", () async {
ListenSpy spy = await bindMock();
Expand Down Expand Up @@ -223,6 +220,61 @@ void useLibraryTest() {
expect(spy.bindPort, 1000);
});
});

group("close", () {
test("should be close a connected connection", () async {
ConnectSpy spy = await connectWithMock(address: "127.0.0.1", port: 1000);
when(spy.socket.close()).thenAnswer((_) => Future.value(null));

await spy.tcp.close();
verify(spy.socket.close());
});

test("should be close a listened connection", () async {
ListenSpy spy = await bindMock();
await listen(spy);
when(spy.socket.close()).thenAnswer((_) => Future.value(null));
when(spy.serverSocket.close()).thenAnswer((_) => Future.value(spy.serverSocket));

await spy.tcp.close();
verify(spy.socket.close());
});

test("should be close a bound server connection", () async {
ListenSpy spy = await bindMock();
await listen(spy);

when(spy.socket.close()).thenAnswer((_) => Future.value(null));
when(spy.serverSocket.close()).thenAnswer((_) => Future.value(spy.serverSocket));

await spy.tcp.close();
verify(spy.serverSocket.close());
});

test("should be close listened connections", () async {
ListenSpy spy = await bindMock();
await listen(spy);
await listen(spy);

when(spy.socket.close()).thenAnswer((_) => Future.value(null));
when(spy.serverSocket.close()).thenAnswer((_) => Future.value(spy.serverSocket));

await spy.tcp.close();
verify(spy.socket.close()).called(2);
});

test("should be close bound server connections", () async {
ListenSpy spy = await bindMock();
await listen(spy);
await listen(spy);

when(spy.socket.close()).thenAnswer((_) => Future.value(null));
when(spy.serverSocket.close()).thenAnswer((_) => Future.value(spy.serverSocket));

await spy.tcp.close();
verify(spy.serverSocket.close()).called(2);
});
});
}

class StreamSubscriptionStub<T> implements StreamSubscription<T> {
Expand Down

0 comments on commit 4a84eda

Please sign in to comment.