Skip to content

Commit a363daa

Browse files
committed
add unit test for core
1 parent efa90ff commit a363daa

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed

Core/interface/SonicClientBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SonicCMS_Core_SonicClientBase
33

44
#include "FWCore/Concurrency/interface/WaitingTaskWithArenaHolder.h"
5+
#include "FWCore/MessageLogger/interface/MessageLogger.h"
56

67
#include <string>
78
#include <chrono>

Core/test/BuildFile.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<environment>
2+
<bin file="TestIntegration.cpp" name="TestIntegrationSonic">
3+
<flags TEST_RUNNER_ARGS=" /bin/bash SonicCMS/Core/test run_sonic_test.sh"/>
4+
<use name="FWCore/Utilities"/>
5+
</bin>
6+
<library file="SonicDummyProducer.cc">
7+
<flags EDM_PLUGIN="1"/>
8+
<use name="SonicCMS/Core"/>
9+
<use name="DataFormats/TestObjects"/>
10+
</library>
11+
</environment>

Core/test/DummyClient.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef SonicCMS_Core_test_DummyClient
2+
#define SonicCMS_Core_test_DummyClient
3+
4+
#include "FWCore/ParameterSet/interface/ParameterSet.h"
5+
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
6+
#include "SonicCMS/Core/interface/SonicClientSync.h"
7+
#include "SonicCMS/Core/interface/SonicClientPseudoAsync.h"
8+
9+
#include <vector>
10+
#include <thread>
11+
#include <chrono>
12+
13+
template <typename Client>
14+
class DummyClient : public Client {
15+
public:
16+
//constructor
17+
DummyClient(const edm::ParameterSet& params) :
18+
factor_(params.getParameter<int>("factor")),
19+
wait_(params.getParameter<int>("wait"))
20+
{}
21+
22+
//for fillDescriptions
23+
static void fillPSetDescription(edm::ParameterSetDescription& iDesc) {
24+
edm::ParameterSetDescription descClient;
25+
descClient.add<int>("factor",-1);
26+
descClient.add<int>("wait",10);
27+
iDesc.add<edm::ParameterSetDescription>("Client",descClient);
28+
}
29+
30+
protected:
31+
void predictImpl() override {
32+
//simulate a blocking call
33+
std::this_thread::sleep_for(std::chrono::seconds(wait_));
34+
35+
this->output_ = this->input_*factor_;
36+
}
37+
38+
//members
39+
int factor_;
40+
int wait_;
41+
};
42+
43+
typedef DummyClient<SonicClientSync<int>> DummyClientSync;
44+
typedef DummyClient<SonicClientPseudoAsync<int>> DummyClientPseudoAsync;
45+
//test of true Async omitted
46+
47+
#endif

Core/test/SonicDummyProducer.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "DummyClient.h"
2+
#include "SonicCMS/Core/interface/SonicEDProducer.h"
3+
#include "DataFormats/TestObjects/interface/ToyProducts.h"
4+
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
5+
#include "FWCore/Framework/interface/MakerMacros.h"
6+
7+
#include <memory>
8+
9+
namespace edmtest {
10+
template <typename Client>
11+
class SonicDummyProducer : public SonicEDProducer<Client> {
12+
public:
13+
//needed because base class has dependent scope
14+
using typename SonicEDProducer<Client>::Input;
15+
using typename SonicEDProducer<Client>::Output;
16+
explicit SonicDummyProducer(edm::ParameterSet const& cfg) :
17+
SonicEDProducer<Client>(cfg),
18+
input_(cfg.getParameter<int>("input"))
19+
{
20+
this->template produces<IntProduct>();
21+
}
22+
23+
void acquire(edm::Event const& iEvent, edm::EventSetup const& iSetup, Input& iInput) override {
24+
iInput = input_;
25+
}
26+
27+
void produce(edm::Event& iEvent, edm::EventSetup const& iSetup, Output const& iOutput) override {
28+
iEvent.put(std::make_unique<IntProduct>(iOutput));
29+
}
30+
31+
//to ensure distinct cfi names - specialized below
32+
static std::string getCfiName();
33+
static void fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
34+
edm::ParameterSetDescription desc;
35+
Client::fillPSetDescription(desc);
36+
desc.add<int>("input");
37+
descriptions.add(getCfiName(),desc);
38+
}
39+
40+
private:
41+
//members
42+
int input_;
43+
};
44+
45+
typedef SonicDummyProducer<DummyClientSync> SonicDummyProducerSync;
46+
typedef SonicDummyProducer<DummyClientPseudoAsync> SonicDummyProducerPseudoAsync;
47+
48+
template<> std::string SonicDummyProducerSync::getCfiName() { return "SonicDummyProducerSync"; }
49+
template<> std::string SonicDummyProducerPseudoAsync::getCfiName() { return "SonicDummyProducerPseudoAsync"; }
50+
}
51+
52+
using edmtest::SonicDummyProducerSync;
53+
DEFINE_FWK_MODULE(SonicDummyProducerSync);
54+
using edmtest::SonicDummyProducerPseudoAsync;
55+
DEFINE_FWK_MODULE(SonicDummyProducerPseudoAsync);
56+

Core/test/TestIntegration.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "FWCore/Utilities/interface/TestHelper.h"
2+
3+
RUNTEST()

Core/test/run_sonic_test.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
function die { echo "$1": status $2 ; exit $2; }
4+
5+
pushd ${LOCAL_TMP_DIR}
6+
7+
TESTCFG=sonicTest_cfg.py
8+
echo "cmsRun $TESTCFG"
9+
cmsRun --parameter-set ${LOCAL_TEST_DIR}/${TESTCFG} || die "Failed in $TESTCFG" $?
10+
11+
popd

Core/test/sonicTest_cfg.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import FWCore.ParameterSet.Config as cms
2+
3+
process = cms.Process("Test")
4+
5+
process.source = cms.Source("EmptySource")
6+
7+
process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))
8+
9+
process.options = cms.untracked.PSet(
10+
numberOfThreads = cms.untracked.uint32(2),
11+
numberOfStreams = cms.untracked.uint32(0)
12+
)
13+
14+
process.dummySync = cms.EDProducer("SonicDummyProducerSync",
15+
input = cms.int32(1),
16+
Client = cms.PSet(
17+
factor = cms.int32(-1),
18+
wait = cms.int32(10),
19+
),
20+
)
21+
22+
process.dummyPseudoAsync = cms.EDProducer("SonicDummyProducerPseudoAsync",
23+
input = cms.int32(2),
24+
Client = cms.PSet(
25+
factor = cms.int32(2),
26+
wait = cms.int32(10),
27+
),
28+
)
29+
30+
process.task = cms.Task(process.dummySync,process.dummyPseudoAsync)
31+
32+
process.testerSync = cms.EDAnalyzer("IntTestAnalyzer",
33+
valueMustMatch = cms.untracked.int32(-1),
34+
moduleLabel = cms.untracked.string("dummySync"),
35+
)
36+
37+
process.testerPseudoAsync = cms.EDAnalyzer("IntTestAnalyzer",
38+
valueMustMatch = cms.untracked.int32(4),
39+
moduleLabel = cms.untracked.string("dummyPseudoAsync"),
40+
)
41+
42+
process.p1 = cms.Path(process.testerSync, process.task)
43+
process.p2 = cms.Path(process.testerPseudoAsync, process.task)

0 commit comments

Comments
 (0)