comparison UnitTestsSources/MultiThreadingTests.cpp @ 2842:ff0ed5ea9e4e

trailing step in SetOfInstancesJob
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 26 Sep 2018 15:55:42 +0200
parents 925d8dc03a23
children 218e2c864d1d
comparison
equal deleted inserted replaced
2841:bf019ee38498 2842:ff0ed5ea9e4e
144 }; 144 };
145 145
146 146
147 class DummyInstancesJob : public SetOfInstancesJob 147 class DummyInstancesJob : public SetOfInstancesJob
148 { 148 {
149 private:
150 bool trailingStepDone_;
151
149 protected: 152 protected:
150 virtual bool HandleInstance(const std::string& instance) 153 virtual bool HandleInstance(const std::string& instance)
151 { 154 {
152 return (instance != "nope"); 155 return (instance != "nope");
153 } 156 }
154 157
158 virtual bool HandleTrailingStep()
159 {
160 if (HasTrailingStep())
161 {
162 if (trailingStepDone_)
163 {
164 throw OrthancException(ErrorCode_InternalError);
165 }
166 else
167 {
168 trailingStepDone_ = true;
169 return true;
170 }
171 }
172 else
173 {
174 throw OrthancException(ErrorCode_InternalError);
175 }
176 }
177
155 public: 178 public:
156 DummyInstancesJob() 179 DummyInstancesJob(bool hasTrailingStep) :
180 SetOfInstancesJob(hasTrailingStep),
181 trailingStepDone_(false)
157 { 182 {
158 } 183 }
159 184
160 DummyInstancesJob(const Json::Value& value) : 185 DummyInstancesJob(const Json::Value& value) :
161 SetOfInstancesJob(value) 186 SetOfInstancesJob(value)
162 { 187 {
188 if (HasTrailingStep())
189 {
190 trailingStepDone_ = (GetPosition() == GetStepsCount());
191 }
192 else
193 {
194 trailingStepDone_ = false;
195 }
196 }
197
198 bool IsTrailingStepDone() const
199 {
200 return trailingStepDone_;
163 } 201 }
164 202
165 virtual void Stop(JobStopReason reason) 203 virtual void Stop(JobStopReason reason)
166 { 204 {
167 } 205 }
788 std::auto_ptr<IJob> unserialized(unserializer.UnserializeJob(a)); 826 std::auto_ptr<IJob> unserialized(unserializer.UnserializeJob(a));
789 827
790 Json::Value b = 43; 828 Json::Value b = 43;
791 if (unserialized->Serialize(b)) 829 if (unserialized->Serialize(b))
792 { 830 {
793 return CheckSameJson(a, b); 831 return (CheckSameJson(a, b));
832 }
833 else
834 {
835 return false;
836 }
837 }
838 }
839
840
841 static bool CheckIdempotentSetOfInstances(IJobUnserializer& unserializer,
842 SetOfInstancesJob& job)
843 {
844 Json::Value a = 42;
845
846 if (!job.Serialize(a))
847 {
848 return false;
849 }
850 else
851 {
852 std::auto_ptr<SetOfInstancesJob> unserialized
853 (dynamic_cast<SetOfInstancesJob*>(unserializer.UnserializeJob(a)));
854
855 Json::Value b = 43;
856 if (unserialized->Serialize(b))
857 {
858 return (CheckSameJson(a, b) &&
859 job.HasTrailingStep() == unserialized->HasTrailingStep() &&
860 job.GetPosition() == unserialized->GetPosition() &&
861 job.GetInstancesCount() == unserialized->GetInstancesCount() &&
862 job.GetStepsCount() == unserialized->GetStepsCount());
794 } 863 }
795 else 864 else
796 { 865 {
797 return false; 866 return false;
798 } 867 }
954 Json::Value s; 1023 Json::Value s;
955 1024
956 // This tests SetOfInstancesJob 1025 // This tests SetOfInstancesJob
957 1026
958 { 1027 {
959 DummyInstancesJob job; 1028 DummyInstancesJob job(false);
960 job.SetDescription("description"); 1029 job.SetDescription("description");
961 job.AddInstance("hello"); 1030 job.AddInstance("hello");
962 job.AddInstance("nope"); 1031 job.AddInstance("nope");
963 job.AddInstance("world"); 1032 job.AddInstance("world");
964 job.SetPermissive(true); 1033 job.SetPermissive(true);
965 ASSERT_THROW(job.Step(), OrthancException); // Not started yet 1034 ASSERT_THROW(job.Step(), OrthancException); // Not started yet
1035 ASSERT_FALSE(job.HasTrailingStep());
1036 ASSERT_FALSE(job.IsTrailingStepDone());
966 job.Start(); 1037 job.Start();
967 job.Step(); 1038 job.Step();
968 job.Step(); 1039 job.Step();
969 1040
970 { 1041 {
971 DummyUnserializer unserializer; 1042 DummyUnserializer unserializer;
972 ASSERT_TRUE(CheckIdempotentSerialization(unserializer, job)); 1043 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
973 } 1044 }
974 1045
975 ASSERT_TRUE(job.Serialize(s)); 1046 ASSERT_TRUE(job.Serialize(s));
976 } 1047 }
977 1048
1416 DicomModalityStoreJob job(GetContext()); 1487 DicomModalityStoreJob job(GetContext());
1417 job.SetLocalAet("LOCAL"); 1488 job.SetLocalAet("LOCAL");
1418 job.SetRemoteModality(modality); 1489 job.SetRemoteModality(modality);
1419 job.SetMoveOriginator("MOVESCU", 42); 1490 job.SetMoveOriginator("MOVESCU", 42);
1420 1491
1421 ASSERT_TRUE(CheckIdempotentSerialization(unserializer, job)); 1492 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1422 ASSERT_TRUE(job.Serialize(s)); 1493 ASSERT_TRUE(job.Serialize(s));
1423 } 1494 }
1424 1495
1425 { 1496 {
1426 std::auto_ptr<IJob> job; 1497 std::auto_ptr<IJob> job;
1446 peer.SetPkcs11Enabled(true); 1517 peer.SetPkcs11Enabled(true);
1447 1518
1448 OrthancPeerStoreJob job(GetContext()); 1519 OrthancPeerStoreJob job(GetContext());
1449 job.SetPeer(peer); 1520 job.SetPeer(peer);
1450 1521
1451 ASSERT_TRUE(CheckIdempotentSerialization(unserializer, job)); 1522 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1452 ASSERT_TRUE(job.Serialize(s)); 1523 ASSERT_TRUE(job.Serialize(s));
1453 } 1524 }
1454 1525
1455 { 1526 {
1456 std::auto_ptr<IJob> job; 1527 std::auto_ptr<IJob> job;
1471 1542
1472 ResourceModificationJob job(GetContext()); 1543 ResourceModificationJob job(GetContext());
1473 job.SetModification(modification.release(), true); 1544 job.SetModification(modification.release(), true);
1474 job.SetOrigin(DicomInstanceOrigin::FromLua()); 1545 job.SetOrigin(DicomInstanceOrigin::FromLua());
1475 1546
1476 ASSERT_TRUE(CheckIdempotentSerialization(unserializer, job)); 1547 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1477 ASSERT_TRUE(job.Serialize(s)); 1548 ASSERT_TRUE(job.Serialize(s));
1478 } 1549 }
1479 1550
1480 { 1551 {
1481 std::auto_ptr<IJob> job; 1552 std::auto_ptr<IJob> job;
1508 Json::Value t; 1579 Json::Value t;
1509 registry.Serialize(t); 1580 registry.Serialize(t);
1510 ASSERT_TRUE(CheckSameJson(s, t)); 1581 ASSERT_TRUE(CheckSameJson(s, t));
1511 } 1582 }
1512 } 1583 }
1584
1585
1586 TEST(JobsSerialization, TrailingStep)
1587 {
1588 {
1589 Json::Value s;
1590
1591 DummyInstancesJob job(false);
1592 ASSERT_EQ(0, job.GetStepsCount());
1593 ASSERT_EQ(0, job.GetInstancesCount());
1594
1595 job.Start();
1596 ASSERT_EQ(0, job.GetPosition());
1597 ASSERT_FALSE(job.HasTrailingStep());
1598 ASSERT_FALSE(job.IsTrailingStepDone());
1599
1600 {
1601 DummyUnserializer unserializer;
1602 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1603 }
1604
1605 ASSERT_EQ(JobStepCode_Success, job.Step().GetCode());
1606 ASSERT_EQ(1, job.GetPosition());
1607 ASSERT_FALSE(job.IsTrailingStepDone());
1608
1609 {
1610 DummyUnserializer unserializer;
1611 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1612 }
1613
1614 ASSERT_THROW(job.Step(), OrthancException);
1615 }
1616
1617 {
1618 Json::Value s;
1619
1620 DummyInstancesJob job(false);
1621 job.AddInstance("hello");
1622 job.AddInstance("world");
1623 ASSERT_EQ(2, job.GetStepsCount());
1624 ASSERT_EQ(2, job.GetInstancesCount());
1625
1626 job.Start();
1627 ASSERT_EQ(0, job.GetPosition());
1628 ASSERT_FALSE(job.HasTrailingStep());
1629 ASSERT_FALSE(job.IsTrailingStepDone());
1630
1631 {
1632 DummyUnserializer unserializer;
1633 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1634 }
1635
1636 ASSERT_EQ(JobStepCode_Continue, job.Step().GetCode());
1637 ASSERT_EQ(1, job.GetPosition());
1638 ASSERT_FALSE(job.IsTrailingStepDone());
1639
1640 {
1641 DummyUnserializer unserializer;
1642 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1643 }
1644
1645 ASSERT_EQ(JobStepCode_Success, job.Step().GetCode());
1646 ASSERT_EQ(2, job.GetPosition());
1647 ASSERT_FALSE(job.IsTrailingStepDone());
1648
1649 {
1650 DummyUnserializer unserializer;
1651 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1652 }
1653
1654 ASSERT_THROW(job.Step(), OrthancException);
1655 }
1656
1657 {
1658 Json::Value s;
1659
1660 DummyInstancesJob job(true);
1661 ASSERT_EQ(1, job.GetStepsCount());
1662 ASSERT_EQ(0, job.GetInstancesCount());
1663
1664 job.Start();
1665 ASSERT_EQ(0, job.GetPosition());
1666 ASSERT_TRUE(job.HasTrailingStep());
1667 ASSERT_FALSE(job.IsTrailingStepDone());
1668
1669 {
1670 DummyUnserializer unserializer;
1671 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1672 }
1673
1674 ASSERT_EQ(JobStepCode_Success, job.Step().GetCode());
1675 ASSERT_EQ(1, job.GetPosition());
1676 ASSERT_TRUE(job.IsTrailingStepDone());
1677
1678 {
1679 DummyUnserializer unserializer;
1680 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1681 }
1682
1683 ASSERT_THROW(job.Step(), OrthancException);
1684 }
1685
1686 {
1687 Json::Value s;
1688
1689 DummyInstancesJob job(true);
1690 job.AddInstance("hello");
1691 ASSERT_EQ(2, job.GetStepsCount());
1692 ASSERT_EQ(1, job.GetInstancesCount());
1693
1694 job.Start();
1695 ASSERT_EQ(0, job.GetPosition());
1696 ASSERT_TRUE(job.HasTrailingStep());
1697 ASSERT_FALSE(job.IsTrailingStepDone());
1698
1699 {
1700 DummyUnserializer unserializer;
1701 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1702 }
1703
1704 ASSERT_EQ(JobStepCode_Continue, job.Step().GetCode());
1705 ASSERT_EQ(1, job.GetPosition());
1706 ASSERT_FALSE(job.IsTrailingStepDone());
1707
1708 {
1709 DummyUnserializer unserializer;
1710 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1711 }
1712
1713 ASSERT_EQ(JobStepCode_Success, job.Step().GetCode());
1714 ASSERT_EQ(2, job.GetPosition());
1715 ASSERT_TRUE(job.IsTrailingStepDone());
1716
1717 {
1718 DummyUnserializer unserializer;
1719 ASSERT_TRUE(CheckIdempotentSetOfInstances(unserializer, job));
1720 }
1721
1722 ASSERT_THROW(job.Step(), OrthancException);
1723 }
1724 }