Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement simple mocking framework and test ntcr::StreamSocket shutdown sequence during connection phase using whitebox testing approach #142

Merged
merged 60 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0c84a63
add first working UT for shutdown sequence
smtrfnv Mar 19, 2024
d3b20a5
Add TC23
smtrfnv Mar 20, 2024
2530038
renaming
smtrfnv Mar 20, 2024
adcd76a
introduce one more TC + huge refactoring of createTimer and schedule …
smtrfnv Mar 20, 2024
50e006f
Inject mocked ntsi::StreamSocket into tested ntcr::StreamSocket and u…
smtrfnv Mar 21, 2024
4718de2
Update close mocked method of ntci::Timer
smtrfnv Mar 21, 2024
e5e70d8
Inject socket mock in TC 23
smtrfnv Mar 21, 2024
3268d34
code
smtrfnv Mar 22, 2024
bae1fec
Dynamically deduce number of argumens in macro call
smtrfnv Mar 26, 2024
80af0b1
Mock more methods
smtrfnv Mar 26, 2024
c94625c
use templates where possible
smtrfnv Mar 27, 2024
38d879d
process arg1
smtrfnv Mar 27, 2024
191b2b4
handle void return result correctly
smtrfnv Mar 27, 2024
98dada6
Save ARG1 correctly (remove_cvref)
smtrfnv Mar 28, 2024
3bda560
Save partially developed new approach to mocks
smtrfnv Apr 2, 2024
bd33772
Save todays work
smtrfnv Apr 2, 2024
053f29d
invoke
smtrfnv Apr 3, 2024
d7e89a9
compilation fix Linux
smtrfnv Apr 3, 2024
20b441f
replacing ld mocks with new ones
smtrfnv Apr 4, 2024
557a3df
some more methods are mocked
smtrfnv Apr 4, 2024
fe1d335
more mocks, include LINE into d_invocations name
smtrfnv Apr 4, 2024
8a0fe62
save work
smtrfnv Apr 4, 2024
04341fc
Update how InvocationData is defined
smtrfnv Apr 5, 2024
7f469c3
Prepare to mock methods with 3 arguments
smtrfnv Apr 5, 2024
d707457
mock Resolver methods with 3 args
smtrfnv Apr 5, 2024
43b60c2
bugfix
smtrfnv Apr 5, 2024
2b73003
mock everything possible with current state of the framework
smtrfnv Apr 5, 2024
d208aec
write expectations in an assumed way
smtrfnv Apr 5, 2024
ab07e5f
remove deprecated code
smtrfnv Apr 6, 2024
2e1585d
remove _NEW suffix
smtrfnv Apr 6, 2024
fbb5c0d
push intermediate results
smtrfnv Apr 8, 2024
fb015e2
Found an issue with returning references
smtrfnv Apr 8, 2024
ead4e01
Do not allow to setup extra expectations after ALWAYS was setup
smtrfnv Apr 9, 2024
d3e133c
Ensure that all expectations which were setup are fired
smtrfnv Apr 9, 2024
c97ee5c
Add diagnostic utility (first version)
smtrfnv Apr 9, 2024
5813f67
More advanced diagnostic
smtrfnv Apr 10, 2024
37af287
add more TCs
smtrfnv Apr 10, 2024
03076cf
Write concerns for new tests
smtrfnv Apr 10, 2024
7209712
Wrongly commited file
smtrfnv Apr 10, 2024
9ce2997
Replace override with BSLS_KEYWORD_OVERRIDE
smtrfnv Apr 10, 2024
0687be3
replace lambda with binding to make old compilers happy
smtrfnv Apr 10, 2024
0d85011
Compilation fix for SunOS compiler
Apr 11, 2024
13d994e
Make compiler happy
Apr 11, 2024
caa42b5
Use TypeList as a list of argument types0
smtrfnv Apr 11, 2024
77465e9
use TypeList to shorten InvocationBaseSaveSetArg structure
smtrfnv Apr 11, 2024
20cad7e
Refactor Invocation structure
smtrfnv Apr 12, 2024
79296bf
Specialize Invocation with 0 arguments
smtrfnv Apr 12, 2024
6a961f4
use bsl::type_identity instead of custom TypeToType
smtrfnv Apr 12, 2024
747710b
Place mock of ntci::Resolver in ntcd group
smtrfnv Apr 12, 2024
e09e2b4
Place mock of bdlbb:BlobBufferFactory in ntcd group
smtrfnv Apr 12, 2024
e28bfce
Place mock of ntci::DataPool in ntcd group
smtrfnv Apr 12, 2024
e5aa3d2
Place mock of ntci::Reactor and ntci::Timer in ntcd group
smtrfnv Apr 12, 2024
70b2f5a
Remove changes done by mistake, add new component to targets.cmake an…
smtrfnv Apr 15, 2024
0b17fbe
add some ntccfg_test TCs as usage examples
smtrfnv Apr 15, 2024
46082c7
Fix an issue with setting an argument of a reference type, add more u…
smtrfnv Apr 15, 2024
7050132
add some ntccfg_test TCs as usage examples
smtrfnv Apr 15, 2024
3956990
add some ntccfg_test TCs as usage examples
smtrfnv Apr 15, 2024
2e1dc09
One more TC
smtrfnv Apr 15, 2024
95cdf1e
remove include
smtrfnv Apr 15, 2024
5adad1b
make clang and gcc happy
smtrfnv Apr 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,157 changes: 1,157 additions & 0 deletions groups/ntc/ntccfg/ntccfg_test.h

Large diffs are not rendered by default.

227 changes: 223 additions & 4 deletions groups/ntc/ntccfg/ntccfg_test.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,238 @@ using namespace BloombergLP;
// [ 1]
//-----------------------------------------------------------------------------

namespace mock_test {

class Interface
{
virtual void f() = 0;
virtual int f1() = 0;

virtual void f2(int) = 0;
virtual void f3(int*) = 0;
virtual void f4(int&) = 0;

virtual void f5(int, char) = 0;
virtual void f5(int*, double) = 0;

virtual const bsl::shared_ptr<int>& f6(int*, double&, long) = 0;
};

NTF_MOCK_CLASS(MyMock, Interface)

NTF_MOCK_METHOD(void, f)
NTF_MOCK_METHOD(int, f1)

NTF_MOCK_METHOD(void, f2, int)
NTF_MOCK_METHOD(void, f3, int*)
NTF_MOCK_METHOD(void, f4, int&)

NTF_MOCK_METHOD(void, f5, int, char)
NTF_MOCK_METHOD(void, f5, int*, double)

NTF_MOCK_METHOD(const bsl::shared_ptr<int>&, f6, int*, double&, long)

NTF_MOCK_CLASS_END;

}

NTCCFG_TEST_CASE(1)
{
// Concern:
// Plan:
using namespace mock_test;

MyMock mock;
NTF_EXPECT_0(mock, f).ONCE();
mock.f();

NTF_EXPECT_0(mock, f1).ONCE().RETURN(22);
NTF_EXPECT_0(mock, f1).ONCE().RETURN(33);

NTCCFG_TEST_EQ(mock.f1(), 22);
NTCCFG_TEST_EQ(mock.f1(), 33);
}

NTCCFG_TEST_CASE(2)
{
using namespace mock_test;

MyMock mock;

{
// it means we do not case what argument is used when f2 is called
NTF_EXPECT_1(mock, f2, IGNORE_ARG).ONCE();

const int val = 22;
mock.f2(val);

ntccfg::TestAllocator ta;
// here we expect that the argument used to call f2 equals `expected`
const int expected = 22;
NTF_EXPECT_1(mock, f2, NTF_EQ(expected)).ONCE();
mock.f2(val);
}
{
int value = 44;
int* ptr = &value;
int* expected_ptr = ptr;

// expect that argument used to call f3 equals `expected_ptr`
NTF_EXPECT_1(mock, f3, NTF_EQ(expected_ptr)).ONCE();
mock.f3(ptr);

//expect that when argument used to call f3 is dereferenced it equals
//`expected value`
int expected_value = value;
NTF_EXPECT_1(mock, f3, NTF_EQ_DEREF(expected_value)).ONCE();
mock.f3(ptr);

int& ref = value;
NTF_EXPECT_1(mock, f4, NTF_EQ(value)).ONCE();
mock.f4(ref);
}
}

NTCCFG_TEST_CASE(3)
{
using namespace mock_test;

MyMock mock;

{
const int newValue = 55;
// when f3 is called, we do not case what arg value is, but we want to
// dereference it and set it value to `newValue`
NTF_EXPECT_1(mock, f3, IGNORE_ARG)
.ONCE()
.SET_ARG_1(FROM_DEREF(newValue));

int val = 0;
mock.f3(&val);
NTCCFG_TEST_EQ(val, newValue);

// the same can be done with references
NTF_EXPECT_1(mock, f4, IGNORE_ARG).ONCE().SET_ARG_1(FROM(newValue));

int data = 12;
int& data_ref = data;
mock.f4(data_ref);
NTCCFG_TEST_EQ(data, newValue);
}
}

NTCCFG_TEST_CASE(4)
{
using namespace mock_test;

MyMock mock;

{
// an argument can be saved to external variable to later used
int storage = 0;
NTF_EXPECT_1(mock, f2, IGNORE_ARG).ONCE().SAVE_ARG_1(TO(&storage));

int val = 22;
mock.f2(val);

NTCCFG_TEST_EQ(storage, val);
}
{
//the same can be done with raw pointers
int* ptr = 0;
NTF_EXPECT_1(mock, f3, IGNORE_ARG).ONCE().SAVE_ARG_1(TO(&ptr));

int val = 6;
mock.f3(&val);
NTCCFG_TEST_EQ(ptr, &val);

//pointer argument can be dereferenced before saving
int storage = 0;
NTF_EXPECT_1(mock, f3, IGNORE_ARG)
.ONCE()
.SAVE_ARG_1(TO_DEREF(&storage));

mock.f3(&val);
NTCCFG_TEST_EQ(storage, val);
}
{
//the same can be done with references
int storage = 0;
NTF_EXPECT_1(mock, f4, IGNORE_ARG).ONCE().SAVE_ARG_1(TO(&storage));

int val = 7;
mock.f4(val);
NTCCFG_TEST_EQ(storage, val);
}
}

NTCCFG_TEST_CASE(5)
{
using namespace mock_test;

MyMock mock;

{
//for overloaded methods we need to specify type of an argument using
// `_SPEC` addition to NTF_EQ (or IGNORE_ARG_S) macro

char c = 'a';
NTF_EXPECT_2(mock, f5, IGNORE_ARG_S(int), NTF_EQ_SPEC(c, char)).ONCE();

mock.f5(22, c);

int val = 14;
double d = 3.14;
NTF_EXPECT_2(mock,
f5,
NTF_EQ_DEREF_SPEC(val, int*),
NTF_EQ_SPEC(d, double))
.ONCE();

mock.f5(&val, d);
}
}

NTCCFG_TEST_CASE(6)
{
using namespace mock_test;

MyMock mock;
{
//see how references can be returned and multiple arguments
//expectations can be set

bsl::shared_ptr<int> sptr(new int(14));
const bsl::shared_ptr<int>& sptrRef = sptr;

int expectedInt = 22;
double expectedDouble = 7.7;
long expectedLong = 100;
int* ptr = 0;
double newDouble = 8.8;
NTF_EXPECT_3(mock,
f6,
NTF_EQ_DEREF(expectedInt),
NTF_EQ(expectedDouble),
NTF_EQ(expectedLong))
.ONCE()
.SAVE_ARG_1(TO(&ptr))
.SET_ARG_2(FROM(newDouble))
.RETURNREF(sptrRef);

const bsl::shared_ptr<int>& res =
mock.f6(&expectedInt, expectedDouble, expectedLong);
NTCCFG_TEST_EQ(ptr, &expectedInt);
NTCCFG_TEST_EQ(expectedDouble, newDouble);
NTCCFG_TEST_EQ(res, sptrRef);
NTCCFG_TEST_EQ(&res, &sptrRef);
}
NTCCFG_TEST_ASSERT(ta.numBlocksInUse() == 0);
}

NTCCFG_TEST_DRIVER
{
NTCCFG_TEST_REGISTER(1);
NTCCFG_TEST_REGISTER(2);
NTCCFG_TEST_REGISTER(3);
NTCCFG_TEST_REGISTER(4);
NTCCFG_TEST_REGISTER(5);
NTCCFG_TEST_REGISTER(6);
}
NTCCFG_TEST_DRIVER_END;
19 changes: 19 additions & 0 deletions groups/ntc/ntcd/ntcd_blobbufferfactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <ntcd_blobbufferfactory.h>

#include <bsls_ident.h>
BSLS_IDENT_RCSID(ntcd_blobbufferfactory_cpp, "$Id$ $CSID$")
34 changes: 34 additions & 0 deletions groups/ntc/ntcd/ntcd_blobbufferfactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_NTCD_BLOBBUFFERFACTORY
#define INCLUDED_NTCD_BLOBBUFFERFACTORY

#include <bsls_ident.h>
BSLS_IDENT("$Id: $")

#include <bdlbb_blob.h>
#include <ntccfg_test.h>

namespace BloombergLP {
namespace ntcd {

NTF_MOCK_CLASS(BufferFactoryMock, bdlbb::BlobBufferFactory)
NTF_MOCK_METHOD(void, allocate, bdlbb::BlobBuffer*)
NTF_MOCK_CLASS_END;

} // close package namespace
} // close enterprise namespace

#endif
28 changes: 28 additions & 0 deletions groups/ntc/ntcd/ntcd_blobbufferfactory.t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <ntcd_blobbufferfactory.h>

#include <ntccfg_test.h>

NTCCFG_TEST_CASE(1)
{
}

NTCCFG_TEST_DRIVER
{
NTCCFG_TEST_REGISTER(1);
}
NTCCFG_TEST_DRIVER_END;
19 changes: 19 additions & 0 deletions groups/ntc/ntcd/ntcd_datapool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <ntcd_datapool.h>

#include <bsls_ident.h>
BSLS_IDENT_RCSID(ntcd_datapool_cpp, "$Id$ $CSID$")
43 changes: 43 additions & 0 deletions groups/ntc/ntcd/ntcd_datapool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_NTCD_DATAPOOL
#define INCLUDED_NTCD_DATAPOOL

#include <bsls_ident.h>
BSLS_IDENT("$Id: $")

#include <ntci_datapool.h>
#include <ntccfg_test.h>

namespace BloombergLP {
namespace ntcd {

NTF_MOCK_CLASS(DataPoolMock, ntci::DataPool)
NTF_MOCK_METHOD(bsl::shared_ptr<ntsa::Data>, createIncomingData)
NTF_MOCK_METHOD(bsl::shared_ptr<ntsa::Data>, createOutgoingData)
NTF_MOCK_METHOD(bsl::shared_ptr<bdlbb::Blob>, createIncomingBlob)
NTF_MOCK_METHOD(bsl::shared_ptr<bdlbb::Blob>, createOutgoingBlob)
NTF_MOCK_METHOD(void, createIncomingBlobBuffer, bdlbb::BlobBuffer*)
NTF_MOCK_METHOD(void, createOutgoingBlobBuffer, bdlbb::BlobBuffer*)
NTF_MOCK_METHOD_CONST(const bsl::shared_ptr<bdlbb::BlobBufferFactory>&,
incomingBlobBufferFactory)
NTF_MOCK_METHOD_CONST(const bsl::shared_ptr<bdlbb::BlobBufferFactory>&,
outgoingBlobBufferFactory)
NTF_MOCK_CLASS_END;

} // close package namespace
} // close enterprise namespace

#endif
Loading
Loading