DiskWriteThreadPoolTest.cpp 5.9 KB
Newer Older
1
2
3
4
5
6
7
/*
 * @project        The CERN Tape Archive (CTA)
 * @copyright      Copyright(C) 2021 CERN
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
18
19
20
#include "castor/tape/tapeserver/daemon/DiskWriteThreadPool.hpp"
#include "castor/tape/tapeserver/daemon/RecallTaskInjector.hpp"
#include "castor/tape/tapeserver/daemon/RecallReportPacker.hpp"
#include "castor/tape/tapeserver/daemon/ReportPackerInterface.hpp"
Victor Kotlyar's avatar
Victor Kotlyar committed
21
22
#include "common/log/LogContext.hpp"
#include "common/log/StringLogger.hpp"
23
24
#include "castor/tape/tapeserver/daemon/MigrationMemoryManager.hpp"
#include "castor/tape/tapeserver/daemon/MemBlock.hpp"
25
#include "castor/messages/TapeserverProxyDummy.hpp"
26
#include "scheduler/TapeMountDummy.hpp"
27
#include "catalogue/DummyCatalogue.hpp"
28
29
30
#include <gtest/gtest.h>

namespace unitTests{
31
  
32
  class TestingDatabaseRetrieveMount: public cta::SchedulerDatabase::RetrieveMount {
33
    const MountInfo & getMountInfo() override { throw std::runtime_error("Not implemented"); }
34
    std::list<std::unique_ptr<cta::SchedulerDatabase::RetrieveJob> > getNextJobBatch(uint64_t filesRequested, uint64_t bytesRequested, cta::disk::DiskSystemFreeSpaceList & diskSystemFreeSpace, cta::log::LogContext& logContext) override { throw std::runtime_error("Not implemented");}
35

36
    void complete(time_t completionTime) override { throw std::runtime_error("Not implemented"); }
37
    void setDriveStatus(cta::common::dataStructures::DriveStatus status, time_t completionTime, const cta::optional<std::string> & reason) override { throw std::runtime_error("Not implemented"); }
38
    void setTapeSessionStats(const castor::tape::tapeserver::daemon::TapeSessionStats &stats) override { throw std::runtime_error("Not implemented"); }
39
    void flushAsyncSuccessReports(std::list<cta::SchedulerDatabase::RetrieveJob*>& jobsBatch, cta::log::LogContext& lc) override { throw std::runtime_error("Not implemented"); }
40
41
  };
  
42
43
  class TestingRetrieveMount: public cta::RetrieveMount {
  public:
44
    TestingRetrieveMount(cta::catalogue::Catalogue &catalogue, std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm): RetrieveMount(catalogue, std::move(dbrm)) {
45
46
47
48
49
    }
  };
  
  class TestingRetrieveJob: public cta::RetrieveJob {
  public:
50
    TestingRetrieveJob(): cta::RetrieveJob(nullptr,
51
52
    cta::common::dataStructures::RetrieveRequest(), 
    cta::common::dataStructures::ArchiveFile(), 1,
53
    cta::PositioningMethod::ByBlock) {}
54
55
  };
  
56
57

  
58
59
  using namespace castor::tape::tapeserver::daemon;
  using namespace castor::tape::tapeserver::client;
60
  struct MockRecallReportPacker : public RecallReportPacker {
61
    void reportCompletedJob(std::unique_ptr<cta::RetrieveJob> successfulRetrieveJob) {
62
      cta::threading::MutexLocker ml(m_mutex);
63
      completeJobs++;
64
    }
65
    void reportFailedJob(std::unique_ptr<cta::RetrieveJob> failedRetrieveJob) {
66
      cta::threading::MutexLocker ml(m_mutex);
67
68
69
70
      failedJobs++;
    }
    void disableBulk() {}
    void reportEndOfSession() {
71
      cta::threading::MutexLocker ml(m_mutex);
72
73
74
      endSessions++;
    }
    void reportEndOfSessionWithErrors(const std::string msg, int error_code) {
75
      cta::threading::MutexLocker ml(m_mutex);
76
      endSessionsWithError++;
77
    }
Victor Kotlyar's avatar
Victor Kotlyar committed
78
    MockRecallReportPacker(cta::RetrieveMount *rm, cta::log::LogContext lc):
79
80
      RecallReportPacker(rm,lc), completeJobs(0), failedJobs(0),
      endSessions(0), endSessionsWithError(0) {}
81
    cta::threading::Mutex m_mutex;
82
83
84
85
    int completeJobs;
    int failedJobs;
    int endSessions;
    int endSessionsWithError;
86
87
  };
  
88
  struct MockTaskInjector : public RecallTaskInjector{
89
90
91
92
93
    MOCK_METHOD3(requestInjection, void(int maxFiles, int maxBlocks, bool lastCall));
  };
  
  TEST(castor_tape_tapeserver_daemon, DiskWriteThreadPoolTest){
    using ::testing::_;
94
    
95
    cta::log::StringLogger log("dummy","castor_tape_tapeserver_daemon_DiskWriteThreadPoolTest",cta::log::DEBUG);
Victor Kotlyar's avatar
Victor Kotlyar committed
96
    cta::log::LogContext lc(log);
97
    
98
    std::unique_ptr<cta::SchedulerDatabase::RetrieveMount> dbrm(new TestingDatabaseRetrieveMount);
99
100
    std::unique_ptr<cta::catalogue::Catalogue> catalogue(new cta::catalogue::DummyCatalogue);
    TestingRetrieveMount trm(*catalogue, std::move(dbrm));
101
    MockRecallReportPacker report(&trm,lc);    
102
    
103
    RecallMemoryManager mm(10,100,lc);
104
    
105
    castor::messages::TapeserverProxyDummy tspd;
106
107
    cta::TapeMountDummy tmd;
    RecallWatchDog rwd(1,1,tspd,tmd,"", lc);
108
    
109
    DiskWriteThreadPool dwtp(2,report,rwd,lc,"/dev/null", 0);
110
111
    dwtp.startThreads();
       
112
113
    for(int i=0;i<5;++i){
      std::unique_ptr<TestingRetrieveJob> fileToRecall(new TestingRetrieveJob());
114
115
116
      fileToRecall->retrieveRequest.archiveFileID = i+1;
      fileToRecall->retrieveRequest.dstURL = "/dev/null";
      fileToRecall->selectedCopyNb=1;
117
118
119
      cta::common::dataStructures::TapeFile tf;
      tf.copyNb = 1;
      fileToRecall->archiveFile.tapeFiles.push_back(tf);
120
      fileToRecall->selectedTapeFile().blockId = 1;
121
122
123
124
125
126
127
128
      DiskWriteTask* t=new DiskWriteTask(fileToRecall.release(),mm);
      MemBlock* mb=mm.getFreeBlock();
      mb->m_fileid=i+1;
      mb->m_fileBlock=0;
      t->pushDataBlock(mb);
      t->pushDataBlock(NULL);
      dwtp.push(t);
    }
129
130
     
    dwtp.finish();
131
132
133
    dwtp.waitThreads();
    ASSERT_EQ(5, report.completeJobs);
    ASSERT_EQ(1, report.endSessions);
134
  }
135

136
137
}