Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(853)

Side by Side Diff: mojo/edk/system/message_pipe_unittest.cc

Issue 1785843002: [mojo] Implement pipe fusion API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "mojo/edk/system/test_utils.h" 8 #include "mojo/edk/system/test_utils.h"
9 #include "mojo/edk/test/mojo_test_base.h" 9 #include "mojo/edk/test/mojo_test_base.h"
10 #include "mojo/public/c/system/core.h" 10 #include "mojo/public/c/system/core.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 may_discard ? MOJO_READ_MESSAGE_FLAG_MAY_DISCARD : 47 may_discard ? MOJO_READ_MESSAGE_FLAG_MAY_DISCARD :
48 MOJO_READ_MESSAGE_FLAG_NONE); 48 MOJO_READ_MESSAGE_FLAG_NONE);
49 } 49 }
50 50
51 MojoHandle pipe0_, pipe1_; 51 MojoHandle pipe0_, pipe1_;
52 52
53 private: 53 private:
54 DISALLOW_COPY_AND_ASSIGN(MessagePipeTest); 54 DISALLOW_COPY_AND_ASSIGN(MessagePipeTest);
55 }; 55 };
56 56
57 using FuseMessagePipeTest = test::MojoTestBase;
58
57 TEST_F(MessagePipeTest, WriteData) { 59 TEST_F(MessagePipeTest, WriteData) {
58 ASSERT_EQ(MOJO_RESULT_OK, 60 ASSERT_EQ(MOJO_RESULT_OK,
59 WriteMessage(pipe0_, kHelloWorld, sizeof(kHelloWorld))); 61 WriteMessage(pipe0_, kHelloWorld, sizeof(kHelloWorld)));
60 } 62 }
61 63
62 // Tests: 64 // Tests:
63 // - only default flags 65 // - only default flags
64 // - reading messages from a port 66 // - reading messages from a port
65 // - when there are no/one/two messages available for that port 67 // - when there are no/one/two messages available for that port
66 // - with buffer size 0 (and null buffer) -- should get size 68 // - with buffer size 0 (and null buffer) -- should get size
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 ReadMessageWithHandles(h, buffers, kPingPongHandlesPerIteration); 484 ReadMessageWithHandles(h, buffers, kPingPongHandlesPerIteration);
483 } 485 }
484 WriteMessage(h, "quit", 4); 486 WriteMessage(h, "quit", 4);
485 END_CHILD() 487 END_CHILD()
486 for (size_t i = 0; i < kPingPongHandlesPerIteration; ++i) 488 for (size_t i = 0; i < kPingPongHandlesPerIteration; ++i)
487 MojoClose(buffers[i]); 489 MojoClose(buffers[i]);
488 } 490 }
489 491
490 #endif // !defined(OS_IOS) 492 #endif // !defined(OS_IOS)
491 493
494 TEST_F(FuseMessagePipeTest, Basic) {
495 // Test that we can fuse pipes and they still work.
496
497 MojoHandle a, b, c, d;
498 CreateMessagePipe(&a, &b);
499 CreateMessagePipe(&c, &d);
500
501 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c));
502
503 // Handles b and c should be closed.
504 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b));
505 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c));
506
507 const std::string kTestMessage1 = "Hello, world!";
508 const std::string kTestMessage2 = "Goodbye, world!";
509
510 WriteMessage(a, kTestMessage1);
511 EXPECT_EQ(kTestMessage1, ReadMessage(d));
512
513 WriteMessage(d, kTestMessage2);
514 EXPECT_EQ(kTestMessage2, ReadMessage(a));
515
516 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
517 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
518 }
519
520 TEST_F(FuseMessagePipeTest, FuseAfterPeerWrite) {
521 // Test that messages written before fusion are eventually delivered.
522
523 MojoHandle a, b, c, d;
524 CreateMessagePipe(&a, &b);
525 CreateMessagePipe(&c, &d);
526
527 const std::string kTestMessage1 = "Hello, world!";
528 const std::string kTestMessage2 = "Goodbye, world!";
529 WriteMessage(a, kTestMessage1);
530 WriteMessage(d, kTestMessage2);
531
532 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c));
533
534 // Handles b and c should be closed.
535 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b));
536 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c));
537
538 EXPECT_EQ(kTestMessage1, ReadMessage(d));
539 EXPECT_EQ(kTestMessage2, ReadMessage(a));
540
541 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
542 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
543 }
544
545 TEST_F(FuseMessagePipeTest, NoFuseAfterWrite) {
546 // Test that a pipe endpoint which has been written to cannot be fused.
547
548 MojoHandle a, b, c, d;
549 CreateMessagePipe(&a, &b);
550 CreateMessagePipe(&c, &d);
551
552 WriteMessage(b, "shouldn't have done that!");
553 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, MojoFuseMessagePipes(b, c));
554
555 // Handles b and c should be closed.
556 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b));
557 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c));
558
559 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
560 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
561 }
562
563 TEST_F(FuseMessagePipeTest, NoFuseSelf) {
564 // Test that a pipe's own endpoints can't be fused together.
565
566 MojoHandle a, b;
567 CreateMessagePipe(&a, &b);
568
569 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, MojoFuseMessagePipes(a, b));
570
571 // Handles a and b should be closed.
572 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(a));
573 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b));
574 }
575
576 TEST_F(FuseMessagePipeTest, FuseInvalidArguments) {
577 MojoHandle a, b, c, d;
578 CreateMessagePipe(&a, &b);
579 CreateMessagePipe(&c, &d);
580
581 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
582
583 // Can't fuse an invalid handle.
584 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoFuseMessagePipes(b, c));
585
586 // Can't fuse a non-message pipe handle.
587 MojoHandle e, f;
588 CreateDataPipe(&e, &f, 16);
589
590 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoFuseMessagePipes(e, c));
591
592 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
593 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(c));
594 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
595 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(e));
596 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(f));
597 }
598
599 TEST_F(FuseMessagePipeTest, FuseAfterPeerClosure) {
600 // Test that peer closure prior to fusion can still be detected after fusion.
601
602 MojoHandle a, b, c, d;
603 CreateMessagePipe(&a, &b);
604 CreateMessagePipe(&c, &d);
605
606 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
607 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c));
608
609 // Handles b and c should be closed.
610 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b));
611 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c));
612
613 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
614 MOJO_DEADLINE_INDEFINITE, nullptr));
615
616 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
617 }
618
619 TEST_F(FuseMessagePipeTest, FuseAfterPeerWriteAndClosure) {
620 // Test that peer write and closure prior to fusion still results in the
621 // both message arrival and awareness of peer closure.
622
623 MojoHandle a, b, c, d;
624 CreateMessagePipe(&a, &b);
625 CreateMessagePipe(&c, &d);
626
627 const std::string kTestMessage = "ayyy lmao";
628 WriteMessage(a, kTestMessage);
629 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(a));
630
631 EXPECT_EQ(MOJO_RESULT_OK, MojoFuseMessagePipes(b, c));
632
633 // Handles b and c should be closed.
634 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(b));
635 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(c));
636
637 EXPECT_EQ(kTestMessage, ReadMessage(d));
638 EXPECT_EQ(MOJO_RESULT_OK, MojoWait(d, MOJO_HANDLE_SIGNAL_PEER_CLOSED,
639 MOJO_DEADLINE_INDEFINITE, nullptr));
640
641 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(d));
642 }
643
492 } // namespace 644 } // namespace
493 } // namespace edk 645 } // namespace edk
494 } // namespace mojo 646 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698