RecallTaskInjectorTest.cpp 8.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/****************************************************************************** 
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * 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 2
 * of the License, or (at your option) any later version.
 * 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.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * 
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/
23
24

#include "castor/legacymsg/RmcProxyDummy.hpp"
25
#include "castor/log/StringLogger.hpp"
26
27
28
#include "castor/mediachanger/MediaChangerFacade.hpp"
#include "castor/mediachanger/MmcProxyDummy.hpp"
#include "castor/messages/AcsProxyDummy.hpp"
29
#include "castor/messages/TapeserverProxyDummy.hpp"
30
#include "castor/server/ProcessCapDummy.hpp"
31
32
33
34
#include "castor/tape/tapeserver/daemon/DiskWriteThreadPool.hpp"
#include "castor/tape/tapeserver/daemon/RecallTaskInjector.hpp"
#include "castor/tape/tapeserver/daemon/TapeServerReporter.hpp"
#include "castor/tape/tapeserver/daemon/TapeReadSingleThread.hpp"
35
#include "castor/tape/tapeserver/daemon/TpconfigLine.hpp"
36
#include "castor/tape/tapeserver/drive/FakeDrive.hpp"
37
#include "castor/utils/utils.hpp"
38
39
40

#include <gtest/gtest.h>

41
42
43
44
using namespace castor::tape::tapeserver::daemon;
using namespace castor::tape;
const int blockSize=4096;

45
namespace unitTests
46
{
47
  
48
49
50
51
52
53
54
55
56
57
58
  class castor_tape_tapeserver_daemonTest: public ::testing::Test {
  protected:

    void SetUp() {
    }

    void TearDown() {
    }

    class MockRetrieveJob: public cta::RetrieveJob {
    public:
59
60
61
62
      MockRetrieveJob(): cta::RetrieveJob(*((cta::RetrieveMount *)NULL),
    cta::ArchiveFile(), 
    std::string(), cta::NameServerTapeFile(),
    cta::PositioningMethod::ByBlock) {}
63

64
    ~MockRetrieveJob() throw() {}
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106


      MOCK_METHOD2(complete, void(const uint32_t checksumOfTransfer, const uint64_t fileSizeOfTransfer));
      MOCK_METHOD1(failed, void(const cta::exception::Exception &ex));
    }; // class MockRetrieveJob

    class MockRetrieveMount: public cta::RetrieveMount {
    public:
      MockRetrieveMount(int nbRecallJobs) {
        createRetrieveJobs(nbRecallJobs);
      }

      ~MockRetrieveMount() throw() {
      }

      std::unique_ptr<cta::RetrieveJob> getNextJob() {
        internalGetNextJob();
        if(m_jobs.empty()) {
          return std::unique_ptr<cta::RetrieveJob>();
        } else {
          std::unique_ptr<cta::RetrieveJob> job =  std::move(m_jobs.front());
          m_jobs.pop_front();
          return job;
        }
      }

      MOCK_METHOD0(internalGetNextJob, cta::RetrieveJob*());
      MOCK_METHOD0(complete, void());

    private:

      std::list<std::unique_ptr<cta::RetrieveJob>> m_jobs;

      void createRetrieveJobs(const unsigned int nbJobs) {
        for(unsigned int i = 0; i < nbJobs; i++) {
          m_jobs.push_back(std::unique_ptr<cta::RetrieveJob>(
            new MockRetrieveJob()));
        }
      }
    }; // class MockRetrieveMount

  }; // class castor_tape_tapeserver_daemonTest
107
108

  class FakeDiskWriteThreadPool: public DiskWriteThreadPool
109
  {
110
111
112
113
114
115
116
117
118
  public:
    using DiskWriteThreadPool::m_tasks;
    FakeDiskWriteThreadPool(castor::log::LogContext & lc):
      DiskWriteThreadPool(1,*((RecallReportPacker*)NULL),
      *((RecallWatchDog*)NULL),lc, "RFIO","/dev/null",0){}
    virtual ~FakeDiskWriteThreadPool() {};
  };

  class FakeSingleTapeReadThread : public TapeSingleThreadInterface<TapeReadTask>
119
  {
120
121
122
  public:
    using TapeSingleThreadInterface<TapeReadTask>::m_tasks;

123
    FakeSingleTapeReadThread(tapeserver::drive::DriveInterface& drive,
124
      castor::mediachanger::MediaChangerFacade & mc,
125
126
127
      tapeserver::daemon::TapeServerReporter & tsr,
      const tapeserver::daemon::VolumeInfo& volInfo, 
      castor::server::ProcessCap& cap,
128
129
130
131
132
133
134
135
136
      castor::log::LogContext & lc):
    TapeSingleThreadInterface<TapeReadTask>(drive, mc, tsr, volInfo,cap, lc){}

    ~FakeSingleTapeReadThread(){
      const unsigned int size= m_tasks.size();
      for(unsigned int i=0;i<size;++i){
        delete m_tasks.pop();
      }
    }
137
138
    
    virtual void run () 
139
    {
140
      m_tasks.push(NULL);
141
    }
142
    
143
144
145
146
147
148
149
    virtual void push(TapeReadTask* t){
      m_tasks.push(t);
    }

    virtual void countTapeLogError(const std::string & error) {};
  };

150
  TEST_F(castor_tape_tapeserver_daemonTest, RecallTaskInjectorNominal) {
151
152
    const int nbJobs=15;
    const int maxNbJobsInjectedAtOnce = 6;
153
154
    castor::log::StringLogger log("castor_tape_tapeserver_daemon_RecallTaskInjectorTest");
    castor::log::LogContext lc(log);
155
    RecallMemoryManager mm(50U, 50U, lc);
156
157
    castor::tape::tapeserver::drive::FakeDrive drive;
    
158
    MockRetrieveMount trm(nbJobs);
159
    EXPECT_CALL(trm, internalGetNextJob()).Times(nbJobs+1);
160
    
161
162
163
164
165
166
167
168
169
170
    FakeDiskWriteThreadPool diskWrite(lc);
    castor::messages::AcsProxyDummy acs;
    castor::mediachanger::MmcProxyDummy mmc;
    castor::legacymsg::RmcProxyDummy rmc;
    castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
    castor::messages::TapeserverProxyDummy initialProcess;
    castor::tape::tapeserver::daemon::VolumeInfo volume;
    volume.density="8000GC";
    volume.vid="V12345";
    volume.mountType=cta::MountType::RETRIEVE;
171
    castor::tape::tapeserver::daemon::TapeServerReporter gsr(initialProcess, DriveConfig(), "0.0.0.0", volume, lc);
172
    castor::server::ProcessCapDummy cap;
173
    FakeSingleTapeReadThread tapeRead(drive, mc, gsr, volume, cap, lc);
174
    tapeserver::daemon::RecallTaskInjector rti(mm, tapeRead, diskWrite, trm, maxNbJobsInjectedAtOnce, blockSize, lc);
175

176
    ASSERT_EQ(true, rti.synchronousInjection());
177
178
    ASSERT_EQ(maxNbJobsInjectedAtOnce+1, diskWrite.m_tasks.size());
    ASSERT_EQ(maxNbJobsInjectedAtOnce+1, tapeRead.m_tasks.size());
179
180
181
182
183

    rti.startThreads();
    rti.requestInjection(false);
    rti.requestInjection(true);
    rti.finish();
184
    ASSERT_NO_THROW(rti.waitThreads());
185
186

    //pushed nbFile*2 files + 1 end of work
187
188
    ASSERT_EQ(nbJobs+1, diskWrite.m_tasks.size());
    ASSERT_EQ(nbJobs+1, tapeRead.m_tasks.size());
189

190
    for(int i=0; i<nbJobs; ++i)
191
192
193
194
195
    {
      delete diskWrite.m_tasks.pop();
      delete tapeRead.m_tasks.pop();
    }

196
    for(int i=0; i<1; ++i)
197
198
199
200
201
    {
      DiskWriteTask* diskWriteTask=diskWrite.m_tasks.pop();
      TapeReadTask* tapeReadTask=tapeRead.m_tasks.pop();

      //static_cast is needed otherwise compilation fails on SL5 with a raw NULL
202
203
      ASSERT_EQ(static_cast<DiskWriteTask*>(NULL), diskWriteTask);
      ASSERT_EQ(static_cast<TapeReadTask*>(NULL), tapeReadTask);
204
205
206
207
      delete diskWriteTask;
      delete tapeReadTask;
    }
  }
208
209
  
  TEST_F(castor_tape_tapeserver_daemonTest, RecallTaskInjectorNoFiles) {
210
211
    castor::log::StringLogger log("castor_tape_tapeserver_daemon_RecallTaskInjectorTest");
    castor::log::LogContext lc(log);
212
    RecallMemoryManager mm(50U, 50U, lc);
213
    castor::tape::tapeserver::drive::FakeDrive drive;
214
215
    
    MockRetrieveMount trm(0);
216
    EXPECT_CALL(trm, internalGetNextJob()).Times(1); //no work: single call to getnextjob
217
    
218
219
220
221
222
223
224
225
226
227
228
    FakeDiskWriteThreadPool diskWrite(lc);
    castor::messages::AcsProxyDummy acs;
    castor::mediachanger::MmcProxyDummy mmc;
    castor::legacymsg::RmcProxyDummy rmc;
    castor::mediachanger::MediaChangerFacade mc(acs, mmc, rmc);
    castor::messages::TapeserverProxyDummy initialProcess;  
    castor::tape::tapeserver::daemon::VolumeInfo volume;
    volume.density="8000GC";
    volume.vid="V12345";
    volume.mountType=cta::MountType::RETRIEVE;
    castor::server::ProcessCapDummy cap;
229
230
    castor::tape::tapeserver::daemon::TapeServerReporter tsr(initialProcess, DriveConfig(), "0.0.0.0", volume, lc);  
    FakeSingleTapeReadThread tapeRead(drive, mc, tsr, volume, cap, lc);
231

232
    tapeserver::daemon::RecallTaskInjector rti(mm, tapeRead, diskWrite, trm, 6, blockSize, lc);
233

234
235
236
    ASSERT_EQ(false, rti.synchronousInjection());
    ASSERT_EQ(0U, diskWrite.m_tasks.size());
    ASSERT_EQ(0U, tapeRead.m_tasks.size());
237
  }
238
}