| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/metrics/statistics_recorder.h" | 14 #include "base/metrics/statistics_recorder.h" |
| 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/test/histogram_tester.h" | 16 #include "base/test/histogram_tester.h" |
| 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/threading/thread.h" |
| 16 #include "base/trace_event/process_memory_dump.h" | 19 #include "base/trace_event/process_memory_dump.h" |
| 17 #include "sql/connection.h" | 20 #include "sql/connection.h" |
| 18 #include "sql/connection_memory_dump_provider.h" | 21 #include "sql/connection_memory_dump_provider.h" |
| 19 #include "sql/correct_sql_test_base.h" | 22 #include "sql/correct_sql_test_base.h" |
| 20 #include "sql/meta_table.h" | 23 #include "sql/meta_table.h" |
| 21 #include "sql/statement.h" | 24 #include "sql/statement.h" |
| 22 #include "sql/test/error_callback_support.h" | 25 #include "sql/test/error_callback_support.h" |
| 23 #include "sql/test/scoped_error_ignorer.h" | 26 #include "sql/test/scoped_error_ignorer.h" |
| 24 #include "sql/test/test_helpers.h" | 27 #include "sql/test/test_helpers.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 1455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1481 | 1484 |
| 1482 // With no key, map everything and create the key. | 1485 // With no key, map everything and create the key. |
| 1483 // TODO(shess): This really should be "maps everything after validating it", | 1486 // TODO(shess): This really should be "maps everything after validating it", |
| 1484 // but that is more complicated to structure. | 1487 // but that is more complicated to structure. |
| 1485 ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); | 1488 ASSERT_GT(db().GetAppropriateMmapSize(), kMmapAlot); |
| 1486 ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); | 1489 ASSERT_TRUE(MetaTable::GetMmapStatus(&db(), &mmap_status)); |
| 1487 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); | 1490 ASSERT_EQ(MetaTable::kMmapSuccess, mmap_status); |
| 1488 } | 1491 } |
| 1489 #endif | 1492 #endif |
| 1490 | 1493 |
| 1494 namespace { |
| 1495 |
| 1496 class Demo { |
| 1497 public: |
| 1498 explicit Demo(scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 1499 : background_task_runner_(task_runner), |
| 1500 weak_ptr_factory_(this) { |
| 1501 } |
| 1502 ~Demo() { |
| 1503 } |
| 1504 |
| 1505 void Ping( |
| 1506 scoped_refptr<base::SingleThreadTaskRunner> callback_runner, |
| 1507 const base::Closure& callback) { |
| 1508 LOG(ERROR) << "Received ping, sleeping"; |
| 1509 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
| 1510 LOG(ERROR) << "Sending pong"; |
| 1511 callback_runner->PostTask(FROM_HERE, callback); |
| 1512 } |
| 1513 |
| 1514 void SendPing() { |
| 1515 // This use of weak pointer is valid, because it will be |
| 1516 // dereferenced when the task is posted back on the current |
| 1517 // thread. |
| 1518 base::Closure callback(base::Bind(&Demo::Pong, |
| 1519 weak_ptr_factory_.GetWeakPtr())); |
| 1520 |
| 1521 // This use of weak pointer is not valid, because it will be |
| 1522 // dereferenced when the task is posted on the background thread. |
| 1523 // It fails the sequence_checker_.CalledOnValidSequencedThread() |
| 1524 // DCHECK in WeakReference::Flag::IsValid(). |
| 1525 background_task_runner_->PostTask( |
| 1526 FROM_HERE, |
| 1527 base::Bind(&Demo::Ping, weak_ptr_factory_.GetWeakPtr(), |
| 1528 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 1529 } |
| 1530 |
| 1531 void Pong() { |
| 1532 LOG(ERROR) << "Received pong, sleeping"; |
| 1533 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
| 1534 LOG(ERROR) << "Sending ping"; |
| 1535 SendPing(); |
| 1536 } |
| 1537 |
| 1538 void Start() { |
| 1539 LOG(ERROR) << "Sending initial ping"; |
| 1540 SendPing(); |
| 1541 } |
| 1542 |
| 1543 private: |
| 1544 scoped_refptr<base::SingleThreadTaskRunner> background_task_runner_; |
| 1545 |
| 1546 base::WeakPtrFactory<Demo> weak_ptr_factory_; |
| 1547 }; |
| 1548 |
| 1549 } // namespace |
| 1550 |
| 1551 TEST_F(SQLConnectionTest, WeakDemo) { |
| 1552 base::MessageLoop message_loop; |
| 1553 |
| 1554 base::Thread background_thread("demo"); |
| 1555 ASSERT_TRUE(background_thread.Start()); |
| 1556 |
| 1557 Demo d(background_thread.task_runner()); |
| 1558 d.Start(); |
| 1559 } |
| 1560 |
| 1491 } // namespace sql | 1561 } // namespace sql |
| OLD | NEW |