Skip to content

Commit

Permalink
Make TestTcp retry TCP initialization (#15824)
Browse files Browse the repository at this point in the history
* Retry workaround for TCP failure on mac

* Restyle
  • Loading branch information
andy31415 authored and pull[bot] committed Nov 30, 2023
1 parent d4b5832 commit ff6e83c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/transport/raw/tests/TestTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/UnitTestUtils.h>
#include <system/SystemLayer.h>
#include <transport/TransportMgr.h>
#include <transport/raw/TCP.h>
Expand Down Expand Up @@ -107,6 +108,27 @@ class MockTransportMgrDelegate : public chip::TransportMgrDelegate
void InitializeMessageTest(TCPImpl & tcp, const IPAddress & addr)
{
CHIP_ERROR err = tcp.Init(Transport::TcpListenParameters(mContext.GetTCPEndPointManager()).SetAddressType(addr.Type()));

// retry a few times in case the port is somehow in use.
// this is a WORKAROUND for flaky testing if we run tests very fast after each other.
// in that case, a port could be in a WAIT state.
//
// What may be happening:
// - We call InitializeMessageTest several times in this unit test
// - closing sockets takes a while (FIN-wait or similar)
// - trying InitializeMessageTest to take the same port right after may fail
//
// The tests may be run with a 0 port (to self select an active port) however I have not
// validated that this works and we need a followup for it
//
// TODO: stop using fixed ports.
for (int i = 0; (i < 50) && (err != CHIP_NO_ERROR); i++)
{
ChipLogProgress(NotSpecified, "RETRYING tcp initialization");
chip::test_utils::SleepMillis(100);
err = tcp.Init(Transport::TcpListenParameters(mContext.GetTCPEndPointManager()).SetAddressType(addr.Type()));
}

NL_TEST_ASSERT(mSuite, err == CHIP_NO_ERROR);

mTransportMgrBase.SetSessionManager(this);
Expand Down

0 comments on commit ff6e83c

Please sign in to comment.