grpc 0.9.0

The official Rust implementation of gRPC: a high performance, open source, universal RPC framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/*
 *
 * Copyright 2026 gRPC authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

use std::collections::HashMap;
use std::collections::HashSet;
use std::hash::Hash;
use std::hash::Hasher;
use std::sync::Arc;
use std::sync::Mutex;

use crate::StatusCodeError;
use crate::StatusError;
use crate::client::load_balancing::ChannelController;
use crate::client::load_balancing::LbPolicy;
use crate::client::load_balancing::LbState;
use crate::client::load_balancing::PickResult;
use crate::client::load_balancing::Picker;
use crate::client::load_balancing::subchannel::ForwardingSubchannel;
use crate::client::load_balancing::subchannel::Subchannel;
use crate::client::load_balancing::subchannel::SubchannelState;
use crate::client::load_balancing::subchannel::WeakSubchannel;
use crate::client::name_resolution::Address;
use crate::client::name_resolution::ResolverUpdate;
use crate::core::RequestHeaders;

/// Implements subchannel sharing for T.  Whenever T creates a subchannel, this
/// policy wraps what the channel returns, and if another subchannel is created
/// for the same address, the first subchannel will be reused to back the second
/// subchannel.
#[derive(Debug)]
pub(crate) struct SubchannelSharing<T> {
    delegate: T,
    inner: Arc<Mutex<Inner>>,
}

impl<T> SubchannelSharing<T> {
    pub(crate) fn new(delegate: T) -> Self {
        Self {
            delegate,
            inner: Arc::new(Mutex::new(Inner {
                subchannels_by_address: HashMap::new(),
                subchannels_int_to_ext: HashMap::new(),
            })),
        }
    }
}

#[derive(Debug)]
struct Inner {
    subchannels_by_address: HashMap<Address, Arc<dyn Subchannel>>,
    subchannels_int_to_ext:
        HashMap<Arc<dyn Subchannel>, (SubchannelState, HashSet<WeakSubchannel>)>,
}

impl<T: LbPolicy> LbPolicy for SubchannelSharing<T> {
    type LbConfig = T::LbConfig;

    fn resolver_update(
        &mut self,
        update: ResolverUpdate,
        config: Option<&T::LbConfig>,
        channel_controller: &mut dyn ChannelController,
    ) -> Result<(), String> {
        let mut channel_controller = SharingChannelController {
            balancer_inner: self.inner.clone(),
            delegate: channel_controller,
        };
        self.delegate
            .resolver_update(update, config, &mut channel_controller)
    }

    fn subchannel_update(
        &mut self,
        subchannel: Arc<dyn Subchannel>,
        state: &SubchannelState,
        channel_controller: &mut dyn ChannelController,
    ) {
        let mut channel_controller = SharingChannelController {
            balancer_inner: self.inner.clone(),
            delegate: channel_controller,
        };

        let mut inner = self.inner.lock().unwrap();

        // Call subchannel_update for every promotable SharedSubchannel for
        // `subchannel`.  If things cannot be promoted they will be cleaned up
        // by SubchannelSharing's Drop impl.
        let Some((old_state, subchannel_set)) = inner.subchannels_int_to_ext.get_mut(&subchannel)
        else {
            return;
        };

        // Update the stored internal state for future subchannel creation.
        *old_state = state.clone();

        let ext_subchannels: Vec<_> = subchannel_set
            .iter()
            .filter_map(|weak| weak.upgrade())
            .collect();

        // Do not perform the outgoing calls with this lock held as it may need
        // to be reacquired, e.g. if the delegate creates a new subchannel.
        drop(inner);

        for ext in ext_subchannels {
            self.delegate
                .subchannel_update(ext, state, &mut channel_controller)
        }
    }

    fn work(&mut self, channel_controller: &mut dyn ChannelController) {
        let mut channel_controller = SharingChannelController {
            balancer_inner: self.inner.clone(),
            delegate: channel_controller,
        };
        self.delegate.work(&mut channel_controller);
    }

    fn exit_idle(&mut self, channel_controller: &mut dyn ChannelController) {
        let mut channel_controller = SharingChannelController {
            balancer_inner: self.inner.clone(),
            delegate: channel_controller,
        };
        self.delegate.exit_idle(&mut channel_controller);
    }
}

#[derive(Debug)]
struct SharedSubchannel {
    delegate: Arc<dyn Subchannel>,
    balancer_inner: Arc<Mutex<Inner>>,
}

impl PartialEq for SharedSubchannel {
    fn eq(&self, other: &Self) -> bool {
        // self.real_subchannel == other.real_subchannel hits
        // https://github.com/rust-lang/rust/issues/31740 bug and
        // &self.real_subchannel == &other.real_subchannel makes clippy
        // complain.
        PartialEq::eq(&self.delegate, &other.delegate)
    }
}

impl Eq for SharedSubchannel {}

impl Hash for SharedSubchannel {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.delegate.hash(state);
    }
}

impl ForwardingSubchannel for SharedSubchannel {
    fn delegate(&self) -> &Arc<dyn Subchannel> {
        &self.delegate
    }
}

impl Drop for SharedSubchannel {
    fn drop(&mut self) {
        let mut inner = self.balancer_inner.lock().unwrap();
        let ext_subchannels = &mut inner
            .subchannels_int_to_ext
            .get_mut(&self.delegate)
            .expect("should always find internal subchannel")
            .1;
        // Note that since we iterate over every weak subchannel, performance is
        // predicated on not extensively sharing subchannels.  If subchannels
        // are commonly shared many times, we could instead store an
        // Option<WeakSubchannel> for ourselves inside SharedSubchannel to allow
        // us to do something like `ext_subchannels.remove(&self.weak_self)`
        // (which would be constructed using Arc::new_cyclic).
        ext_subchannels.retain(|weak| weak.strong_count() != 0);
        if ext_subchannels.is_empty() {
            // This is the last external subchannel using this internal
            // subchannel.  Drop the internal subchannel.
            inner.subchannels_int_to_ext.remove(&self.delegate);
            inner
                .subchannels_by_address
                .remove(&self.delegate.address());
        }
    }
}

struct SharingChannelController<'a> {
    balancer_inner: Arc<Mutex<Inner>>,
    delegate: &'a mut dyn ChannelController,
}

impl<'a> ChannelController for SharingChannelController<'a> {
    fn new_subchannel(&mut self, address: &Address) -> (Arc<dyn Subchannel>, SubchannelState) {
        let mut inner = self.balancer_inner.lock().unwrap();

        // Find the existing internal subchannel with this address, or create
        // one and insert it into the map if one has not been created yet.
        let mut new_state = None;
        let int_subchannel = inner
            .subchannels_by_address
            .entry(address.clone())
            .or_insert_with(|| {
                let (new_sc, state) = self.delegate.new_subchannel(address);
                new_state = Some(state);
                new_sc
            })
            .clone();

        let ext_subchannel: Arc<dyn Subchannel> = Arc::new(SharedSubchannel {
            delegate: int_subchannel.clone(),
            balancer_inner: self.balancer_inner.clone(),
        });

        // Insert a weak reference to this new external subchannel into the
        // int->ext map.
        let entry = inner
            .subchannels_int_to_ext
            .entry(int_subchannel)
            .or_insert_with(|| (new_state.unwrap(), HashSet::new()));

        entry.1.insert((&ext_subchannel).into());

        (ext_subchannel, entry.0.clone())
    }

    fn update_picker(&mut self, mut update: LbState) {
        update.picker = UnwrapPicker::new_arc(update.picker);
        self.delegate.update_picker(update);
    }

    fn request_resolution(&mut self) {
        self.delegate.request_resolution();
    }
}

#[derive(Debug)]
struct UnwrapPicker {
    delegate: Arc<dyn Picker>,
}

impl UnwrapPicker {
    fn new_arc(delegate: Arc<dyn Picker>) -> Arc<Self> {
        Arc::new(Self { delegate })
    }
}

impl Picker for UnwrapPicker {
    fn pick(&self, request: &RequestHeaders) -> PickResult {
        let result = self.delegate.pick(request);
        match result {
            PickResult::Pick(mut pick) => {
                let Some(subchannel) = pick.subchannel.downcast_ref::<SharedSubchannel>() else {
                    return PickResult::Fail(StatusError::new(
                        StatusCodeError::Internal,
                        format!(
                            "received unexpected subchannel type: {:?}",
                            pick.subchannel.type_id()
                        ),
                    ));
                };
                pick.subchannel = subchannel.delegate.clone();
                PickResult::Pick(pick)
            }
            _ => result,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fmt::Debug;
    use std::sync::Arc;
    use std::sync::Mutex;
    use std::sync::mpsc;

    use super::*;
    use crate::client::ConnectivityState;
    use crate::client::load_balancing::LbPolicy;
    use crate::client::load_balancing::LbPolicyOptions;
    use crate::client::load_balancing::Pick;
    use crate::client::load_balancing::PickResult;
    use crate::client::load_balancing::Picker;
    use crate::client::load_balancing::subchannel::SubchannelState;
    use crate::client::load_balancing::test_utils::StubPolicy;
    use crate::client::load_balancing::test_utils::StubPolicyFuncs;
    use crate::client::load_balancing::test_utils::TestChannelController;
    use crate::client::load_balancing::test_utils::TestEvent;
    use crate::client::load_balancing::test_utils::TestWorkScheduler;
    use crate::client::load_balancing::test_utils::new_request_headers;
    use crate::client::name_resolution::Address;
    use crate::client::name_resolution::ResolverUpdate;
    use crate::metadata::MetadataMap;
    use crate::rt::default_runtime;

    fn test_lb_policy_options(tx_events: mpsc::Sender<TestEvent>) -> LbPolicyOptions {
        LbPolicyOptions {
            work_scheduler: Arc::new(TestWorkScheduler { tx_events }),
            runtime: default_runtime(),
        }
    }

    // Tests that a single subchannel creation is properly forwarded to the
    // underlying channel controller and the created shared subchannel seen by
    // the delegate policy contains the real one.
    #[test]
    fn test_single_subchannel() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };

        let sc_out = Arc::new(Mutex::new(None));
        let sc_out_clone = sc_out.clone();

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    let addr = Address {
                        address: "127.0.0.1:80".to_string().into(),
                        ..Default::default()
                    };
                    let sc = cc.new_subchannel(&addr).0;
                    *sc_out_clone.lock().unwrap() = Some(sc);
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        sharing.work(&mut cc);

        let event = rx_events.recv().unwrap();
        let TestEvent::NewSubchannel(internal_sc) = event else {
            panic!("expected NewSubchannel")
        };

        let external_sc = sc_out.lock().unwrap().take().unwrap();
        let shared = external_sc.downcast_ref::<SharedSubchannel>().unwrap();
        assert!(Arc::ptr_eq(&shared.delegate, &internal_sc));
    }

    // Tests that when a delegate policy creates multiple subchannels with the
    // same address, they share the same delegate subchannel from the underlying
    // channel controller.
    #[test]
    fn test_multiple_subchannels_same_address() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };

        let sc_out1 = Arc::new(Mutex::new(None));
        let sc_out1_clone = sc_out1.clone();
        let sc_out2 = Arc::new(Mutex::new(None));
        let sc_out2_clone = sc_out2.clone();

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    let addr = Address {
                        address: "127.0.0.1:80".to_string().into(),
                        ..Default::default()
                    };
                    *sc_out1_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                    *sc_out2_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        sharing.work(&mut cc);

        // Confirm that only one new_subchannel was seen by the underlying
        // channel controller.
        let event = rx_events.recv().unwrap();
        let TestEvent::NewSubchannel(internal_sc) = event else {
            panic!("expected NewSubchannel")
        };
        assert!(rx_events.try_recv().is_err());

        // Confirm that both SharedSubchannels seen by the delegate are unique
        // but share the same underlying subchannel.
        let external_sc1 = sc_out1.lock().unwrap().take().unwrap();
        let external_sc2 = sc_out2.lock().unwrap().take().unwrap();

        let shared1 = external_sc1.downcast_ref::<SharedSubchannel>().unwrap();
        let shared2 = external_sc2.downcast_ref::<SharedSubchannel>().unwrap();

        assert!(Arc::ptr_eq(&shared1.delegate, &internal_sc));
        assert!(Arc::ptr_eq(&shared2.delegate, &internal_sc));
        assert!(!Arc::ptr_eq(&external_sc1, &external_sc2));
    }

    // Tests that when the delegate creates subchannels with different
    // addresses, they get different internal subchannels.
    #[test]
    fn test_multiple_subchannels_different_addresses() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };

        let sc_out1 = Arc::new(Mutex::new(None));
        let sc_out1_clone = sc_out1.clone();
        let sc_out2 = Arc::new(Mutex::new(None));
        let sc_out2_clone = sc_out2.clone();

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    let addr1 = Address {
                        address: "127.0.0.1:80".to_string().into(),
                        ..Default::default()
                    };
                    let addr2 = Address {
                        address: "127.0.0.2:80".to_string().into(),
                        ..Default::default()
                    };
                    *sc_out1_clone.lock().unwrap() = Some(cc.new_subchannel(&addr1).0);
                    *sc_out2_clone.lock().unwrap() = Some(cc.new_subchannel(&addr2).0);
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        sharing.work(&mut cc);

        // Verify that two new_subchannel calls occurred.
        let event1 = rx_events.recv().unwrap();
        let event2 = rx_events.recv().unwrap();
        assert!(matches!(event1, TestEvent::NewSubchannel(_)));
        assert!(matches!(event2, TestEvent::NewSubchannel(_)));

        assert!(rx_events.try_recv().is_err());

        // Verify that the two subchannels contain different delegates.
        let external_sc1 = sc_out1.lock().unwrap().take().unwrap();
        let external_sc2 = sc_out2.lock().unwrap().take().unwrap();

        let shared1 = external_sc1.downcast_ref::<SharedSubchannel>().unwrap();
        let shared2 = external_sc2.downcast_ref::<SharedSubchannel>().unwrap();

        assert!(!Arc::ptr_eq(&shared1.delegate, &shared2.delegate));
    }

    // Tests that when subchannels are dropped, they are removed from the
    // sharing map.
    fn test_subchannel_cleanup_on_drop() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };

        let update_calls = Arc::new(Mutex::new(0));
        let update_calls_clone = update_calls.clone();

        let sc_out1 = Arc::new(Mutex::new(None));
        let sc_out1_clone = sc_out1.clone();
        let sc_out2 = Arc::new(Mutex::new(None));
        let sc_out2_clone = sc_out2.clone();
        let sc_out3 = Arc::new(Mutex::new(None));
        let sc_out3_clone = sc_out3.clone();

        let work_calls = Arc::new(Mutex::new(0));
        let work_calls_clone = work_calls.clone();

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    let addr = Address {
                        address: "127.0.0.1:80".to_string().into(),
                        ..Default::default()
                    };
                    let mut num_calls = work_calls_clone.lock().unwrap();
                    if *num_calls == 0 {
                        *sc_out1_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                        *sc_out2_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                    } else if *num_calls == 1 {
                        *sc_out3_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                    }
                    *num_calls += 1;
                })),
                subchannel_update: Some(Arc::new(move |_data, _sc, _state, _cc| {
                    *update_calls_clone.lock().unwrap() += 1;
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        // The first call to work should create sc1 and sc2.
        sharing.work(&mut cc);
        let _ = rx_events.recv().unwrap();

        let external_sc1 = sc_out1.lock().unwrap().take().unwrap();
        let external_sc2 = sc_out2.lock().unwrap().take().unwrap();

        let internal_sc = external_sc1
            .downcast_ref::<SharedSubchannel>()
            .unwrap()
            .delegate
            .clone();
        let state = SubchannelState::idle();

        // Perform a subchannel update and confirm that two calls are made to
        // the delegate.
        sharing.subchannel_update(internal_sc.clone(), &state, &mut cc);
        assert_eq!(*update_calls.lock().unwrap(), 2);

        // Drop one external subchannel.
        drop(external_sc1);

        // Perform a subchannel update and confirm that only one call is made.
        *update_calls.lock().unwrap() = 0;
        sharing.subchannel_update(internal_sc.clone(), &state, &mut cc);
        assert_eq!(*update_calls.lock().unwrap(), 1);

        // We should have 4 strong references to the internal subchannel: ours,
        // external_sc2, and the maps.
        assert_eq!(Arc::strong_count(&internal_sc), 4);

        // Drop the other subchannel.
        drop(external_sc2);

        // Now there should be only our reference left to the internal
        // subchannel: ours.
        assert_eq!(Arc::strong_count(&internal_sc), 1);

        // Perform a subchannel update and confirm zero calls are made.
        *update_calls.lock().unwrap() = 0;
        sharing.subchannel_update(internal_sc.clone(), &state, &mut cc);
        assert_eq!(*update_calls.lock().unwrap(), 0);

        // Create a subchannel with the same address again and confirm that a
        // new underlying subchannel is created.
        sharing.work(&mut cc);
        let event = rx_events.recv().unwrap();
        assert!(matches!(event, TestEvent::NewSubchannel(_)));

        let external_sc3 = sc_out3.lock().unwrap().take().unwrap();
        let shared_sc3 = external_sc3.downcast_ref::<SharedSubchannel>().unwrap();

        // Confirm a new subchannel was created.
        assert!(!Arc::ptr_eq(&shared_sc3.delegate, &internal_sc));
    }

    // Tests that single subchannel updates are sent to the delegate for every
    // duplicated shared subchannel.
    #[test]
    fn test_subchannel_update_broadcasts() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };

        let update_calls = Arc::new(Mutex::new(0));
        let update_calls_clone = update_calls.clone();

        let sc_out1 = Arc::new(Mutex::new(None));
        let sc_out1_clone = sc_out1.clone();
        let sc_out2 = Arc::new(Mutex::new(None));
        let sc_out2_clone = sc_out2.clone();

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    let addr = Address {
                        address: "127.0.0.1:80".to_string().into(),
                        ..Default::default()
                    };
                    *sc_out1_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                    *sc_out2_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                })),
                subchannel_update: Some(Arc::new(move |_data, _sc, _state, _cc| {
                    *update_calls_clone.lock().unwrap() += 1;
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        sharing.work(&mut cc);
        let _ = rx_events.recv().unwrap();

        let external_sc1 = sc_out1.lock().unwrap().take().unwrap();
        let external_sc2 = sc_out2.lock().unwrap().take().unwrap();

        let internal_sc = external_sc1
            .downcast_ref::<SharedSubchannel>()
            .unwrap()
            .delegate
            .clone();
        let state = SubchannelState::idle();

        // Verify that two delegated update calls are made.
        sharing.subchannel_update(internal_sc.clone(), &state, &mut cc);
        assert_eq!(*update_calls.lock().unwrap(), 2);

        // Drop one and verify that one delegated update call is made.
        drop(external_sc1);
        sharing.subchannel_update(internal_sc, &state, &mut cc);
        assert_eq!(*update_calls.lock().unwrap(), 3);
    }

    // Tests that the picker properly unwraps the shared subchannel into the
    // underlying subchannel.
    #[test]
    fn test_picker_unwraps_shared_subchannel() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };

        let sc_out = Arc::new(Mutex::new(None));
        let sc_out_clone = sc_out.clone();

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    let addr = Address {
                        address: "127.0.0.1:80".to_string().into(),
                        ..Default::default()
                    };
                    let sc = cc.new_subchannel(&addr).0;
                    *sc_out_clone.lock().unwrap() = Some(sc.clone());

                    #[derive(Debug)]
                    struct MockPicker {
                        sc: Arc<dyn Subchannel>,
                    }
                    impl Picker for MockPicker {
                        fn pick(&self, _req: &RequestHeaders) -> PickResult {
                            PickResult::Pick(Pick {
                                subchannel: self.sc.clone(),
                                metadata: MetadataMap::new(),
                                on_complete: None,
                            })
                        }
                    }

                    cc.update_picker(LbState {
                        connectivity_state: ConnectivityState::Ready,
                        picker: Arc::new(MockPicker { sc }),
                    });
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        sharing.work(&mut cc);
        let _ = rx_events.recv().unwrap();

        let event = rx_events.recv().unwrap();
        let TestEvent::UpdatePicker(state) = event else {
            panic!("expected UpdatePicker")
        };

        let req = new_request_headers();
        let result = state.picker.pick(&req);
        let PickResult::Pick(pick) = result else {
            panic!("expected Pick")
        };

        let external_sc = sc_out.lock().unwrap().take().unwrap();
        let shared = external_sc.downcast_ref::<SharedSubchannel>().unwrap();

        assert!(Arc::ptr_eq(&pick.subchannel, &shared.delegate));
    }

    // Tests that update/work/exit_idle methods are delegated appropriately and
    // resolve_now is delegated back to the channel.
    #[test]
    fn test_delegates_other_methods() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };

        let called = Arc::new(Mutex::new(vec![]));

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                resolver_update: Some(Arc::new({
                    let called_clone = called.clone();
                    move |_data, _update, _config, _cc| {
                        called_clone.lock().unwrap().push("resolver_update");
                        Ok(())
                    }
                })),
                work: Some(Arc::new({
                    let called_clone = called.clone();
                    move |_data, cc| {
                        called_clone.lock().unwrap().push("work");
                        cc.request_resolution();
                    }
                })),
                exit_idle: Some(Arc::new({
                    let called_clone = called.clone();
                    move |_data, _cc| called_clone.lock().unwrap().push("exit_idle")
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        let update = ResolverUpdate::default();
        sharing.resolver_update(update, None, &mut cc).unwrap();
        sharing.work(&mut cc);
        sharing.exit_idle(&mut cc);

        assert_eq!(
            *called.lock().unwrap(),
            vec!["resolver_update", "work", "exit_idle"]
        );

        let event = rx_events.recv().unwrap();
        assert!(matches!(event, TestEvent::RequestResolution));
    }

    // Tests that nothing deadlocks when the channel_controller is called during
    // an incoming subchannel update, which could happen if the map lock is held
    // during the call and a subchannel is created.
    #[test]
    fn test_subchannel_update_deadlock() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };
        let sc_out1 = Arc::new(Mutex::new(None));
        let sc_out1_clone = sc_out1.clone();

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    let addr = Address {
                        address: "127.0.0.1:80".to_string().into(),
                        ..Default::default()
                    };
                    *sc_out1_clone.lock().unwrap() = Some(cc.new_subchannel(&addr).0);
                })),
                subchannel_update: Some(Arc::new(move |_data, _sc, _state, cc| {
                    // Try to create a new subchannel while handling the update.
                    // If the lock is held, this will deadlock!
                    let addr = Address {
                        address: "127.0.0.2:80".to_string().into(),
                        ..Default::default()
                    };
                    cc.new_subchannel(&addr);
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        sharing.work(&mut cc);
        let event = rx_events.recv().unwrap();
        let TestEvent::NewSubchannel(_int_sc) = event else {
            panic!("expected NewSubchannel")
        };

        let external_sc = sc_out1.lock().unwrap().take().unwrap();
        let internal_sc = external_sc
            .downcast_ref::<SharedSubchannel>()
            .unwrap()
            .delegate
            .clone();

        let state = SubchannelState::idle();
        // This should not deadlock.
        sharing.subchannel_update(internal_sc.clone(), &state, &mut cc);
    }

    // Tests that a shared subchannel's correct state is returned by
    // new_subchannel.
    #[test]
    fn test_new_subchannel_state() {
        let (tx_events, rx_events) = mpsc::channel();
        let mut cc = TestChannelController {
            tx_events: tx_events.clone(),
        };
        let (tx_work, rx_work) =
            mpsc::channel::<Box<dyn FnOnce(&mut dyn ChannelController) + Send>>();
        // Wrap rx_work in a mutex to allow the stub work Fn() closure to access
        // it mutably.
        let rx_work = Mutex::new(rx_work);

        let mock = StubPolicy::new(
            StubPolicyFuncs {
                work: Some(Arc::new(move |_data, cc| {
                    (rx_work.lock().unwrap().recv().unwrap())(cc);
                })),
                ..Default::default()
            },
            test_lb_policy_options(tx_events.clone()),
        );

        let mut sharing = SubchannelSharing::new(mock);

        let addr = Address {
            address: "127.0.0.2:80".to_string().into(),
            ..Default::default()
        };

        let sc1 = Arc::new(Mutex::new(None));

        // Create the first subchannel
        let sc1_clone = sc1.clone();
        let addr_clone = addr.clone();
        tx_work
            .send(Box::new(move |cc| {
                let (sc, state) = cc.new_subchannel(&addr_clone);
                assert_eq!(state.connectivity_state, ConnectivityState::Idle);
                *sc1_clone.lock().unwrap() = Some(sc);
            }))
            .unwrap();
        sharing.work(&mut cc);

        let event = rx_events.recv().unwrap();
        let TestEvent::NewSubchannel(int_sc) = event else {
            panic!("expected NewSubchannel")
        };

        // Update the state to Connecting.
        sharing.subchannel_update(int_sc.clone(), &SubchannelState::connecting(), &mut cc);

        // Create a second subchannel for the address and verify that the state
        // is also Connecting.
        let addr_clone = addr.clone();
        tx_work
            .send(Box::new(move |cc| {
                let (sc, state) = cc.new_subchannel(&addr_clone);
                assert_eq!(state.connectivity_state, ConnectivityState::Connecting);
            }))
            .unwrap();
        sharing.work(&mut cc);

        // Update the state to Ready.
        sharing.subchannel_update(int_sc.clone(), &SubchannelState::ready(), &mut cc);

        // Create another subchannel for the address and verify that the state
        // is now Ready.
        let addr_clone = addr.clone();
        tx_work
            .send(Box::new(move |cc| {
                let (sc, state) = cc.new_subchannel(&addr_clone);
                assert_eq!(state.connectivity_state, ConnectivityState::Ready);
            }))
            .unwrap();
        sharing.work(&mut cc);
    }
}