From 8bdd703ebdfb6d3f8e98999edc3ae67f45538d13 Mon Sep 17 00:00:00 2001 From: "R.Noto" Date: Sun, 2 Jul 2023 12:16:10 +0900 Subject: [PATCH 1/3] fix donot close bug --- android/local.properties | 4 +- example/lib/main.dart | 11 ++-- example/pubspec.lock | 6 +-- lib/camel.dart | 4 ++ lib/tcp.dart | 21 +++++++- test/tcp_test.dart | 108 +++++++++++++++++++++++++++++---------- 6 files changed, 115 insertions(+), 39 deletions(-) diff --git a/android/local.properties b/android/local.properties index 1a02ba0..836cfb1 100644 --- a/android/local.properties +++ b/android/local.properties @@ -1,2 +1,2 @@ -sdk.dir=C:\\Users\\R.NOTO\\AppData\\Local\\Android\\sdk -flutter.sdk=C:\\Users\\R.NOTO\\fvm\\versions\\3.10.3 \ No newline at end of file +sdk.dir=C:/Users/R.NOTO/AppData/Local/Android/Sdk +flutter.sdk=D:\\create\\flutter\\camel\\.fvm\\flutter_sdk \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 7ec57ee..e7997dc 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -61,17 +61,18 @@ class _NoCommandPage extends State { } } - void _send() { + void _send() async { final Camel sender = Camel(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 @@ -155,17 +156,19 @@ class _CommandPage extends State implements Command { .listen(SocketConnectionPoint(address: myAddress, port: port))) {} } - void _send() { + void _send() async{ final Camel sender = Camel(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 diff --git a/example/pubspec.lock b/example/pubspec.lock index 785b491..fa2640c 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -47,7 +47,7 @@ packages: path: ".." relative: true source: path - version: "0.0.1" + version: "1.0.0" characters: dependency: transitive description: @@ -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 diff --git a/lib/camel.dart b/lib/camel.dart index 2aacb30..d69ff23 100644 --- a/lib/camel.dart +++ b/lib/camel.dart @@ -31,4 +31,8 @@ class Camel { yield data; } } + + void close(){ + communicator.close(); + } } diff --git a/lib/tcp.dart b/lib/tcp.dart index c53cc3f..56ceae7 100644 --- a/lib/tcp.dart +++ b/lib/tcp.dart @@ -44,14 +44,26 @@ class CommunicateDataTcp { class Tcp implements Communicator { final Map _receiveBuffers = {}; + final List _connection = []; + final List _servers = []; @override Future 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 send(CommunicateData data) async { @@ -66,8 +78,10 @@ class Tcp implements Communicator { final StreamController> 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. @@ -110,6 +124,7 @@ class Tcp implements Communicator { {dynamic sourceAddress, int sourcePort, Duration? timeout}) _connect = Socket.connect; + Function( dynamic address, int port, { @@ -118,6 +133,7 @@ class Tcp implements Communicator { bool shared, }) _bind = ServerSocket.bind; + @visibleForTesting void setConnectMock( Function(dynamic, int, {dynamic sourceAddress, int sourcePort, Duration? timeout}) @@ -127,6 +143,7 @@ class Tcp implements Communicator { } } + @visibleForTesting void setBindMock( Function( dynamic address, diff --git a/test/tcp_test.dart b/test/tcp_test.dart index 821819c..b196c61 100644 --- a/test/tcp_test.dart +++ b/test/tcp_test.dart @@ -75,9 +75,10 @@ Future 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 bindMock( dynamic address, @@ -122,6 +123,27 @@ void constructSocketMock(ListenSpy spy, List receiveData) { }); } +Future listen(ListenSpy spy, {bool waitReceive=false, Function(CommunicateData)? callback, List? receiveDatas}) async { + constructListenMock(spy, receiveDatas ?? []); + listenFunc() async { + await for (CommunicateData 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 { @@ -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(); @@ -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 implements StreamSubscription { From 061e0c2b958e4b515e4b6285de48526af3d1a91b Mon Sep 17 00:00:00 2001 From: "R.Noto" Date: Sun, 2 Jul 2023 12:24:13 +0900 Subject: [PATCH 2/3] delete local.properties --- .gitignore | 2 ++ android/local.properties | 2 -- example/.gitignore | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 android/local.properties diff --git a/.gitignore b/.gitignore index 959100c..8026f2b 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ migrate_working_dir/ build/ .fvm/ + +/android/local.properties diff --git a/android/local.properties b/android/local.properties deleted file mode 100644 index 836cfb1..0000000 --- a/android/local.properties +++ /dev/null @@ -1,2 +0,0 @@ -sdk.dir=C:/Users/R.NOTO/AppData/Local/Android/Sdk -flutter.sdk=D:\\create\\flutter\\camel\\.fvm\\flutter_sdk \ No newline at end of file diff --git a/example/.gitignore b/example/.gitignore index 24476c5..dc3775e 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -42,3 +42,4 @@ app.*.map.json /android/app/debug /android/app/profile /android/app/release +/android/local.properties From e92cc6ef85372caf5867ae4b288fb4e3fdca8a64 Mon Sep 17 00:00:00 2001 From: "R.Noto" Date: Sun, 2 Jul 2023 12:29:59 +0900 Subject: [PATCH 3/3] release 1.3.2 --- CHANGELOG.md | 2 ++ pubspec.yaml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0785342..73d1c7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/pubspec.yaml b/pubspec.yaml index dbdb960..4ea2140 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: