1 // Copyright 2019 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/ErrorInjector.h" 16 17 #include "common/Assert.h" 18 #include "dawn_native/DawnNative.h" 19 20 namespace dawn_native { 21 22 namespace { 23 24 bool sIsEnabled = false; 25 uint64_t sNextIndex = 0; 26 uint64_t sInjectedFailureIndex = 0; 27 bool sHasPendingInjectedError = false; 28 29 } // anonymous namespace 30 EnableErrorInjector()31 void EnableErrorInjector() { 32 sIsEnabled = true; 33 } 34 DisableErrorInjector()35 void DisableErrorInjector() { 36 sIsEnabled = false; 37 } 38 ClearErrorInjector()39 void ClearErrorInjector() { 40 sNextIndex = 0; 41 sHasPendingInjectedError = false; 42 } 43 ErrorInjectorEnabled()44 bool ErrorInjectorEnabled() { 45 return sIsEnabled; 46 } 47 AcquireErrorInjectorCallCount()48 uint64_t AcquireErrorInjectorCallCount() { 49 uint64_t count = sNextIndex; 50 ClearErrorInjector(); 51 return count; 52 } 53 ShouldInjectError()54 bool ShouldInjectError() { 55 uint64_t index = sNextIndex++; 56 if (sHasPendingInjectedError && index == sInjectedFailureIndex) { 57 sHasPendingInjectedError = false; 58 return true; 59 } 60 return false; 61 } 62 InjectErrorAt(uint64_t index)63 void InjectErrorAt(uint64_t index) { 64 // Only one error can be injected at a time. 65 ASSERT(!sHasPendingInjectedError); 66 sInjectedFailureIndex = index; 67 sHasPendingInjectedError = true; 68 } 69 70 } // namespace dawn_native 71