1 /** @file 2 3 A brief file description 4 5 @section license License 6 7 Licensed to the Apache Software Foundation (ASF) under one 8 or more contributor license agreements. See the NOTICE file 9 distributed with this work for additional information 10 regarding copyright ownership. The ASF licenses this file 11 to you under the Apache License, Version 2.0 (the 12 "License"); you may not use this file except in compliance 13 with the License. You may obtain a copy of the License at 14 15 http://www.apache.org/licenses/LICENSE-2.0 16 17 Unless required by applicable law or agreed to in writing, software 18 distributed under the License is distributed on an "AS IS" BASIS, 19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 See the License for the specific language governing permissions and 21 limitations under the License. 22 */ 23 24 #define LARGE_FILE 10 * 1024 * 1024 25 #define SMALL_FILE 10 * 1024 26 27 #include "main.h" 28 29 class CacheUpdateReadAgain : public CacheTestHandler 30 { 31 public: CacheUpdateReadAgain(size_t size,const char * url)32 CacheUpdateReadAgain(size_t size, const char *url) : CacheTestHandler() 33 { 34 this->_rt = new CacheReadTest(size, this, url); 35 this->_rt->mutex = this->mutex; 36 37 SET_HANDLER(&CacheUpdateReadAgain::start_test); 38 } 39 40 int start_test(int event,void * e)41 start_test(int event, void *e) 42 { 43 REQUIRE(event == EVENT_IMMEDIATE); 44 this_ethread()->schedule_imm(this->_rt); 45 return 0; 46 } 47 48 void handle_cache_event(int event,CacheTestBase * base)49 handle_cache_event(int event, CacheTestBase *base) override 50 { 51 switch (event) { 52 case CACHE_EVENT_OPEN_READ: 53 base->do_io_read(); 54 REQUIRE(base->vc->alternate.get_frag_offset_count() > 8); 55 break; 56 case VC_EVENT_READ_READY: 57 base->reenable(); 58 break; 59 case VC_EVENT_READ_COMPLETE: 60 base->close(); 61 delete this; 62 break; 63 default: 64 REQUIRE(false); 65 break; 66 } 67 } 68 }; 69 70 class CacheUpdate_S_to_L : public CacheTestHandler 71 { 72 public: CacheUpdate_S_to_L(size_t read_size,size_t write_size,const char * url)73 CacheUpdate_S_to_L(size_t read_size, size_t write_size, const char *url) 74 { 75 this->_rt = new CacheReadTest(read_size, this, url); 76 this->_wt = new CacheWriteTest(write_size, this, url); 77 78 this->_rt->mutex = this->mutex; 79 this->_wt->mutex = this->mutex; 80 81 SET_HANDLER(&CacheUpdate_S_to_L::start_test); 82 } 83 84 int start_test(int event,void * e)85 start_test(int event, void *e) 86 { 87 REQUIRE(event == EVENT_IMMEDIATE); 88 this_ethread()->schedule_imm(this->_rt); 89 return 0; 90 } 91 92 void handle_cache_event(int event,CacheTestBase * base)93 handle_cache_event(int event, CacheTestBase *base) override 94 { 95 CacheWriteTest *wt = static_cast<CacheWriteTest *>(this->_wt); 96 switch (event) { 97 case CACHE_EVENT_OPEN_WRITE: 98 base->do_io_write(); 99 break; 100 case VC_EVENT_WRITE_READY: 101 base->reenable(); 102 break; 103 case VC_EVENT_WRITE_COMPLETE: 104 this->_wt->close(); 105 this->_wt = nullptr; 106 delete this; 107 break; 108 case CACHE_EVENT_OPEN_READ: 109 base->do_io_read(); 110 wt->old_info.copy(static_cast<HTTPInfo *>(&base->vc->alternate)); 111 break; 112 case VC_EVENT_READ_READY: 113 base->reenable(); 114 break; 115 case VC_EVENT_READ_COMPLETE: 116 this->_rt->close(); 117 this->_rt = nullptr; 118 this_ethread()->schedule_imm(this->_wt); 119 break; 120 default: 121 REQUIRE(false); 122 break; 123 } 124 } 125 }; 126 127 class CacheUpdateInit : public CacheInit 128 { 129 public: CacheUpdateInit()130 CacheUpdateInit() {} 131 int cache_init_success_callback(int event,void * e)132 cache_init_success_callback(int event, void *e) override 133 { 134 CacheTestHandler *h = new CacheTestHandler(SMALL_FILE, "http://www.scw11.com"); 135 CacheUpdate_S_to_L *update = new CacheUpdate_S_to_L(SMALL_FILE, LARGE_FILE, "http://www.scw11.com"); 136 CacheUpdateReadAgain *read = new CacheUpdateReadAgain(LARGE_FILE, "http://www.scw11.com"); 137 TerminalTest *tt = new TerminalTest; 138 139 h->add(update); 140 h->add(read); // read again 141 h->add(tt); 142 this_ethread()->schedule_imm(h); 143 delete this; 144 return 0; 145 } 146 }; 147 148 TEST_CASE("cache write -> read", "cache") 149 { 150 init_cache(256 * 1024 * 1024); 151 // large write test 152 CacheUpdateInit *init = new CacheUpdateInit; 153 154 this_ethread()->schedule_imm(init); 155 this_thread()->execute(); 156 } 157