DiskReadTaskTest.cpp 5.11 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
 *****************************************************************************/
David COME's avatar
David COME committed
23
24
#include "castor/tape/tapeserver/daemon/DiskReadTask.hpp"
#include "castor/tape/tapeserver/daemon/DataConsumer.hpp"
25
#include "castor/tape/tapeserver/daemon/MigrationMemoryManager.hpp"
26
#include "castor/tape/tapeserver/daemon/MigrationReportPacker.hpp"
David COME's avatar
David COME committed
27
28
29
#include "castor/tape/tapeserver/daemon/MemBlock.hpp"
#include "castor/log/LogContext.hpp"
#include "castor/log/StringLogger.hpp"
Eric Cano's avatar
Eric Cano committed
30
#include "castor/messages/TapeserverProxyDummy.hpp"
31
32
#include "scheduler/ArchiveMount.hpp"

David COME's avatar
David COME committed
33
34
35
36
37
38
#include <gtest/gtest.h>
#include <fstream>
#include <cmath>
#include <ext/stdio_filebuf.h>

namespace unitTests{
39
  
40
41
  class TestingArchiveJob: public cta::ArchiveJob {
  public:
42
43
44
    TestingArchiveJob(): cta::ArchiveJob(*((cta::ArchiveMount *)NULL), 
        *((cta::NameServer *)NULL), cta::ArchiveFile(), 
        cta::RemotePathAndStatus(), cta::NameServerTapeFile()) {
45
    }
46
47
48
49
50
51
  };
  
  using namespace castor::tape::tapeserver::daemon;
  using namespace castor::tape::diskFile;
  
  struct MockMigrationReportPacker : public MigrationReportPacker {
52
53
54
55
56
    void reportCompletedJob(std::unique_ptr<cta::ArchiveJob> successfulArchiveJob) {}
    void reportFailedJob(std::unique_ptr<cta::ArchiveJob> failedArchiveJob, const castor::exception::Exception& ex) {}
    void reportEndOfSession() {}
    void reportEndOfSessionWithErrors(const std::string msg, int error_code) {}
    void disableBulk() {}
57
    MockMigrationReportPacker(cta::ArchiveMount *rm,castor::log::LogContext lc):
58
      MigrationReportPacker(rm,lc) {}
59
60
  };
  
David COME's avatar
David COME committed
61
  class FakeTapeWriteTask : public  DataConsumer{
62
    castor::server::BlockingQueue<MemBlock*> fifo;
David COME's avatar
David COME committed
63
64
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
    unsigned long m_checksum;
  public:
    FakeTapeWriteTask():m_checksum(Payload::zeroAdler32()){
      
    }
    virtual MemBlock * getFreeBlock() {
      return fifo.pop();
    }
  
    virtual void pushDataBlock(MemBlock *mb) {
      m_checksum = mb->m_payload.adler32(m_checksum);
      fifo.push(mb);
    }
    unsigned long getChecksum() {
      return m_checksum;
    }
  };
 
  template <class T> unsigned long mycopy(T& out,int size){
    std::vector<char> v(size);
    unsigned long checksum = Payload::zeroAdler32();
    std::ifstream in("/dev/urandom", std::ios::in|std::ios::binary);
    in.read(&v[0],size);
    checksum = adler32(checksum,reinterpret_cast<unsigned char*>(&v[0]),size);
    out.write(&v[0],size);
    return checksum;
  }
90
  
Eric Cano's avatar
Eric Cano committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  class MockMigrationWatchDog: public MigrationWatchDog {
  public:
    MockMigrationWatchDog(double periodToReport,double stuckPeriod,
    castor::messages::TapeserverProxy& initialProcess,
    const std::string & driveUnitName,
    castor::log::LogContext lc, double pollPeriod = 0.1): 
      MigrationWatchDog(periodToReport, stuckPeriod, initialProcess, 
          driveUnitName, lc, pollPeriod) {}
  private:

    virtual void logStuckFile() {}

    virtual void run() {}

  };
  
107
108
109
  TEST(castor_tape_tapeserver_daemon, DiskReadTaskTest){
    char path[]="/tmp/testDRT-XXXXXX";
    mkstemp(path);
Eric Cano's avatar
Eric Cano committed
110
    std::string url("file://");
Eric Cano's avatar
Eric Cano committed
111
    url += path;
112
113
114
115
116
117
118
    std::ofstream out(path,std::ios::out | std::ios::binary);
    castor::server::AtomicFlag flag;
    castor::log::StringLogger log("castor_tape_tapeserver_daemon_DiskReadTaskTest");
    castor::log::LogContext lc(log);

    const int blockSize=1500;    
    const int fileSize(1024*2000);
119

120
121
122
123
124
125
    const unsigned long original_checksum = mycopy(out,fileSize);
    castor::tape::tapeserver::daemon::MigrationMemoryManager mm(1,blockSize,lc);

    TestingArchiveJob file;

    file.archiveFile.size = fileSize;
Eric Cano's avatar
Eric Cano committed
126
    file.remotePathAndStatus.path.setPath(url);
127
128
129
130
131
132
133
134
135

    const int blockNeeded=fileSize/mm.blockCapacity()+((fileSize%mm.blockCapacity()==0) ? 0 : 1);
    int value=std::ceil(1024*2000./blockSize);
    ASSERT_EQ(value,blockNeeded);

    FakeTapeWriteTask ftwt;
    ftwt.pushDataBlock(new MemBlock(1,blockSize));
    castor::tape::tapeserver::daemon::DiskReadTask drt(ftwt,&file,blockNeeded,flag);
    DiskFileFactory fileFactory("RFIO","",0);
Eric Cano's avatar
Eric Cano committed
136
137
138
    castor::messages::TapeserverProxyDummy tspd;
    MockMigrationWatchDog mmwd(1.0, 1.0, tspd, "", lc);
    drt.execute(lc,fileFactory,mmwd);
139
140
141
142
143

    ASSERT_EQ(original_checksum,ftwt.getChecksum());
    delete ftwt.getFreeBlock();
  }
}