diff --git a/unit-tests/Makefile.in b/unit-tests/Makefile.in
index e06f9dc..f09294e 100644
--- a/unit-tests/Makefile.in
+++ b/unit-tests/Makefile.in
@@ -48,6 +48,7 @@ TEST_SOURCE=\
unit-tests/array_block_t.cc \
unit-tests/array_t.cc \
unit-tests/base64_t.cc \
+ unit-tests/bcache_t.cc \
unit-tests/block_t.cc \
unit-tests/bitset_t.cc \
unit-tests/btree_t.cc \
diff --git a/unit-tests/bcache_t.cc b/unit-tests/bcache_t.cc
new file mode 100644
index 0000000..67c45e9
--- /dev/null
+++ b/unit-tests/bcache_t.cc
@@ -0,0 +1,70 @@
+// Copyright (C) 2017 Red Hat, Inc. All rights reserved.
+//
+// This file is part of the thin-provisioning-tools source.
+//
+// thin-provisioning-tools 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.
+//
+// thin-provisioning-tools 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 thin-provisioning-tools. If not, see
+// .
+
+#include "gmock/gmock.h"
+#include "block-cache/block_cache.h"
+#include "test_utils.h"
+
+#include
+#include
+#include
+#include
+
+using namespace std;
+using namespace test;
+using namespace testing;
+
+//----------------------------------------------------------------
+
+TEST(BCacheTests, cleaned_on_demand)
+{
+ using namespace bcache;
+
+ unsigned const NR_BLOCKS = 16;
+
+ temp_file tmp("bcache_t", 1);
+ int fd = open(tmp.get_path().c_str(), O_RDWR | O_DIRECT, 0666);
+
+ uint64_t bs = 8;
+ block_cache bc(fd, bs, 64, (bs << SECTOR_SHIFT) * NR_BLOCKS);
+ validator::ptr v(new bcache::noop_validator());
+ for (unsigned i = 0; i < NR_BLOCKS; i++) {
+ block_cache::block &b = bc.get(i, block_cache::GF_DIRTY | block_cache::GF_ZERO, v);
+ b.put();
+ }
+
+ // Default WRITEBACK_LOW_THRESHOLD_PERCENT == 33
+ // For NR_BLOCKS cache entires, only 6 dirty blocks will be submitted,
+ // so the 7th get() doesn't work.
+ list blocks;
+
+ try {
+ for (unsigned i = 0; i < NR_BLOCKS; i++)
+ blocks.push_back(&bc.get(NR_BLOCKS + i, 0, v));
+
+ } catch (...) {
+ for (auto b : blocks)
+ b->put();
+
+ throw;
+ }
+
+ for (auto b : blocks)
+ b->put();
+}
+