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
// Copyright 2020 Cognite AS
//! The primary interface for users of the library.
use std::collections::hash_map::HashMap;
use std::default::Default;
use std::fmt::Debug;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use arc_swap::ArcSwapOption;
use chrono::Utc;
use enum_map::{Enum, EnumMap};
use futures_timer::Delay;
use log::{debug, trace, warn};
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::api::{Feature, Features, Metrics, MetricsBucket, Registration};
use crate::context::Context;
use crate::http::HTTP;
use crate::strategy;

pub struct ClientBuilder {
    enable_str_features: bool,
    interval: u64,
    strategies: HashMap<String, strategy::Strategy>,
}

impl ClientBuilder {
    pub fn into_client<C, F>(
        self,
        api_url: &str,
        app_name: &str,
        instance_id: &str,
        authorization: Option<String>,
    ) -> Result<Client<C, F>, http_client::Error>
    where
        C: http_client::HttpClient + Default,
        F: Enum<CachedFeature> + Debug + DeserializeOwned + Serialize,
    {
        Ok(Client {
            api_url: api_url.into(),
            app_name: app_name.into(),
            enable_str_features: self.enable_str_features,
            instance_id: instance_id.into(),
            interval: self.interval,
            polling: AtomicBool::new(false),
            http: HTTP::new(app_name.into(), instance_id.into(), authorization)?,
            cached_state: ArcSwapOption::from(None),
            strategies: Mutex::new(self.strategies),
        })
    }

    pub fn enable_string_features(mut self) -> Self {
        self.enable_str_features = true;
        self
    }

    pub fn interval(mut self, interval: u64) -> Self {
        self.interval = interval;
        self
    }

    pub fn strategy(mut self, name: &str, strategy: strategy::Strategy) -> Self {
        self.strategies.insert(name.into(), strategy);
        self
    }
}

impl Default for ClientBuilder {
    fn default() -> ClientBuilder {
        let result = ClientBuilder {
            enable_str_features: false,
            interval: 15000,
            strategies: Default::default(),
        };
        result
            .strategy("default", Box::new(&strategy::default))
            .strategy("applicationHostname", Box::new(&strategy::hostname))
            .strategy("default", Box::new(&strategy::default))
            .strategy("gradualRolloutRandom", Box::new(&strategy::random))
            .strategy("gradualRolloutSessionId", Box::new(&strategy::session_id))
            .strategy("gradualRolloutUserId", Box::new(&strategy::user_id))
            .strategy("remoteAddress", Box::new(&strategy::remote_address))
            .strategy("userWithId", Box::new(&strategy::user_with_id))
            .strategy("flexibleRollout", Box::new(&strategy::flexible_rollout))
    }
}

#[derive(Default)]
pub struct CachedFeature {
    strategies: Vec<strategy::Evaluate>,
    // unknown features are tracked for metrics (so the server can see that they
    // are being used). They require specific logic (see is_enabled).
    known: bool,
    // Tracks metrics during a refresh interval. If the AtomicBool updates show
    // to be a contention point then thread-sharded counters with a gather phase
    // on submission will be the next logical progression.
    enabled: AtomicU64,
    disabled: AtomicU64,
}

struct CachedState<F>
where
    F: Enum<CachedFeature>,
{
    start: chrono::DateTime<chrono::Utc>,
    // user supplies F defining the features they need
    // The default value of F is defined as 'fallback to string lookups'.
    features: EnumMap<F, CachedFeature>,
    str_features: HashMap<String, CachedFeature>,
}

pub struct Client<C, F>
where
    C: http_client::HttpClient,
    F: Enum<CachedFeature> + Debug + DeserializeOwned + Serialize,
{
    api_url: String,
    app_name: String,
    enable_str_features: bool,
    instance_id: String,
    interval: u64,
    polling: AtomicBool,
    http: HTTP<C>,
    // known strategies: strategy_name : memoiser
    strategies: Mutex<HashMap<String, strategy::Strategy>>,
    // memoised state: feature_name: [callback, callback, ...]
    cached_state: ArcSwapOption<CachedState<F>>,
}

impl<C, F> Client<C, F>
where
    C: http_client::HttpClient + std::default::Default,
    F: Enum<CachedFeature> + Clone + Debug + DeserializeOwned + Serialize,
{
    pub fn is_enabled(&self, feature_enum: F, context: Option<&Context>, default: bool) -> bool {
        trace!(
            "is_enabled: feature {:?} default {}, context {:?}",
            feature_enum,
            default,
            context
        );
        let cache = self.cached_state.load();
        let cache = if let Some(cache) = &*cache {
            cache
        } else {
            // No API state loaded
            trace!("is_enabled: No API state");
            return false;
        };

        let feature = &cache.features[feature_enum.clone()];
        let default_context: Context = Default::default();
        let context = context.unwrap_or(&default_context);
        for memo in feature.strategies.iter() {
            if memo(context) {
                debug!(
                    "is_enabled: feature {:?} enabled by memo {:p}, context {:?}",
                    feature_enum, memo, context
                );
                feature.enabled.fetch_add(1, Ordering::Relaxed);
                return true;
            } else {
                feature.disabled.fetch_add(1, Ordering::Relaxed);
                trace!(
                    "is_enabled: feature {:?} not enabled by memo {:p}, context {:?}",
                    feature_enum,
                    memo,
                    context
                );
            }
        }
        if !feature.known {
            trace!(
                "is_enabled: Unknown feature {:?}, using default {}",
                feature_enum,
                default
            );
            if default {
                feature.enabled.fetch_add(1, Ordering::Relaxed);
            } else {
                feature.disabled.fetch_add(1, Ordering::Relaxed);
            }
            default
        } else {
            false
        }
    }

    pub fn is_enabled_str(
        &self,
        feature_name: &str,
        context: Option<&Context>,
        default: bool,
    ) -> bool {
        trace!(
            "is_enabled: feature_str {:?} default {}, context {:?}",
            feature_name,
            default,
            context
        );
        assert!(
            self.enable_str_features,
            "String feature lookup not enabled"
        );
        let cache = self.cached_state.load();
        let cache = if let Some(cache) = &*cache {
            cache
        } else {
            // No API state loaded
            trace!("is_enabled: No API state");
            return false;
        };
        if let Some(feature) = cache.str_features.get(feature_name) {
            let default_context: Context = Default::default();
            let context = context.unwrap_or(&default_context);
            for memo in feature.strategies.iter() {
                if memo(context) {
                    debug!(
                        "is_enabled: feature {} enabled by memo {:p}, context {:?}",
                        feature_name, memo, context
                    );
                    feature.enabled.fetch_add(1, Ordering::Relaxed);
                    return true;
                } else {
                    feature.disabled.fetch_add(1, Ordering::Relaxed);
                    trace!(
                        "is_enabled: feature {} not enabled by memo {:p}, context {:?}",
                        feature_name,
                        memo,
                        context
                    );
                }
            }
            if !feature.known {
                trace!(
                    "is_enabled: Unknown feature {}, using default {}",
                    feature_name,
                    default
                );
                if default {
                    feature.enabled.fetch_add(1, Ordering::Relaxed);
                } else {
                    feature.disabled.fetch_add(1, Ordering::Relaxed);
                }
                default
            } else {
                false
            }
        } else {
            trace!(
                "is_enabled: Unknown feature {}, using default {}",
                feature_name,
                default
            );
            // Insert a compiled feature to track metrics.
            self.cached_state
                .rcu(|cached_state: &Option<Arc<CachedState<F>>>| {
                    // Did someone swap None in ?
                    if let Some(cached_state) = cached_state {
                        let cached_state = cached_state.clone();
                        if let Some(feature) = cached_state.str_features.get(feature_name) {
                            // raced with *either* a poll_for_updates() that
                            // added the feature in the API server or another
                            // thread adding this same metric memoisation;
                            // record against metrics here, but still return
                            // default as consistent enough.
                            if default {
                                feature.enabled.fetch_add(1, Ordering::Relaxed);
                            } else {
                                feature.disabled.fetch_add(1, Ordering::Relaxed);
                            }
                            Some(cached_state)
                        } else {
                            // still not present; add it
                            // Build up a new cached state
                            let mut new_state = CachedState {
                                start: cached_state.start,
                                features: EnumMap::new(),
                                str_features: HashMap::new(),
                            };
                            fn cloned_feature(feature: &CachedFeature) -> CachedFeature {
                                CachedFeature {
                                    disabled: AtomicU64::new(
                                        feature.disabled.load(Ordering::Relaxed),
                                    ),
                                    enabled: AtomicU64::new(
                                        feature.enabled.load(Ordering::Relaxed),
                                    ),
                                    known: feature.known,
                                    strategies: feature.strategies.clone(),
                                }
                            };
                            for (key, feature) in &cached_state.features {
                                new_state.features[key] = cloned_feature(&feature);
                            }
                            for (name, feature) in &cached_state.str_features {
                                new_state
                                    .str_features
                                    .insert(name.clone(), cloned_feature(&feature));
                            }
                            let stub_feature = CachedFeature {
                                disabled: AtomicU64::new(if default { 0 } else { 1 }),
                                enabled: AtomicU64::new(if default { 1 } else { 0 }),
                                known: false,
                                strategies: vec![],
                            };
                            new_state
                                .str_features
                                .insert(feature_name.into(), stub_feature);
                            Some(Arc::new(new_state))
                        }
                    } else {
                        None
                    }
                });
            default
        }
    }

    /// Memoize new features into the cached state
    ///
    /// Interior mutability is used, via the arc-swap crate.
    ///
    /// Note that this is primarily public to facilitate benchmarking;
    /// poll_for_updates is the usual way in which memoize will be called.
    pub fn memoize(
        &self,
        features: Vec<Feature>,
    ) -> Result<Option<Metrics>, Box<dyn std::error::Error>> {
        let now = Utc::now();
        trace!("memoize: start with {} features", features.len());
        let source_strategies = self.strategies.lock().unwrap();
        let mut unenumerated_features: HashMap<String, CachedFeature> = HashMap::new();
        let mut cached_features: EnumMap<F, CachedFeature> = EnumMap::new();
        // HashMap<String, Vec<Box<strategy::Evaluate>>> = HashMap::new();
        for feature in features {
            let cached_feature = {
                if !feature.enabled {
                    // no strategies == return false per the unleash example code;
                    let strategies = vec![];
                    CachedFeature {
                        strategies,
                        disabled: AtomicU64::new(0),
                        enabled: AtomicU64::new(0),
                        known: true,
                    }
                } else {
                    // TODO add variant support
                    let mut strategies = vec![];
                    for api_strategy in feature.strategies {
                        if let Some(code_strategy) = source_strategies.get(&api_strategy.name) {
                            strategies.push(code_strategy(api_strategy.parameters));
                        }
                        // Graceful degradation: ignore this unknown strategy.
                        // TODO: add a logging layer and log it.
                    }
                    CachedFeature {
                        strategies,
                        disabled: AtomicU64::new(0),
                        enabled: AtomicU64::new(0),
                        known: true,
                    }
                }
            };
            if let Ok(feature_enum) = serde_plain::from_str::<F>(feature.name.as_str()) {
                cached_features[feature_enum] = cached_feature;
            } else {
                unenumerated_features.insert(feature.name.clone(), cached_feature);
            }
        }
        let new_cache = CachedState {
            start: now,
            features: cached_features,
            str_features: unenumerated_features,
        };
        // Now we have the new cache compiled, swap it in.
        let old = self.cached_state.swap(Some(Arc::new(new_cache)));
        trace!("memoize: swapped memoized state in");
        if let Some(old) = old {
            // send metrics here
            let mut bucket = MetricsBucket {
                start: old.start,
                stop: now,
                toggles: HashMap::new(),
            };
            for (key, feature) in &old.features {
                bucket.toggles.insert(
                    // Is this unwrap safe? Not sure.
                    serde_plain::to_string(&key).unwrap(),
                    [
                        ("yes".into(), feature.enabled.load(Ordering::Relaxed)),
                        ("no".into(), feature.disabled.load(Ordering::Relaxed)),
                    ]
                    .iter()
                    .cloned()
                    .collect(),
                );
            }
            for (name, feature) in &old.str_features {
                bucket.toggles.insert(
                    name.clone(),
                    [
                        ("yes".into(), feature.enabled.load(Ordering::Relaxed)),
                        ("no".into(), feature.disabled.load(Ordering::Relaxed)),
                    ]
                    .iter()
                    .cloned()
                    .collect(),
                );
            }
            let metrics = Metrics {
                app_name: self.app_name.clone(),
                instance_id: self.instance_id.clone(),
                bucket,
            };
            Ok(Some(metrics))
        } else {
            Ok(None)
        }
    }

    /// Query the API endpoint for features and push metrics
    ///
    /// Immediately and then every self.interval milliseconds the API server is
    /// queryed for features and the previous cycles metrics are uploaded.
    ///
    /// May be dropped, or will terminate at the next polling cycle after
    /// stop_poll is called().
    pub async fn poll_for_updates(&self) {
        // TODO: add an event / pipe to permit immediate exit.
        let endpoint = Features::endpoint(&self.api_url);
        let metrics_endpoint = Metrics::endpoint(&self.api_url);
        self.polling.store(true, Ordering::Relaxed);
        loop {
            debug!("poll: retrieving features");
            let res = self.http.get(&endpoint).recv_json().await;
            if let Ok(res) = res {
                let features: Features = res;
                match self.memoize(features.features) {
                    Ok(None) => {}
                    Ok(Some(metrics)) => {
                        let mut metrics_uploaded = false;
                        let req = self.http.post(&metrics_endpoint).body_json(&metrics);
                        if let Ok(req) = req {
                            let res = req.await;
                            if let Ok(res) = res {
                                if res.status().is_success() {
                                    metrics_uploaded = true;
                                    debug!("poll: uploaded feature metrics")
                                }
                            }
                        }
                        if !metrics_uploaded {
                            warn!("poll: error uploading feature metrics");
                        }
                    }
                    Err(_) => {
                        warn!("poll: failed to memoize features");
                    }
                }
            } else {
                warn!("poll: failed to retrieve features");
            }
            Delay::new(Duration::from_millis(self.interval)).await;
            if !self.polling.load(Ordering::Relaxed) {
                return;
            }
        }
    }

    /// Register this client with the API endpoint.
    pub async fn register(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
        let registration = Registration {
            app_name: self.app_name.clone(),
            instance_id: self.instance_id.clone(),
            interval: self.interval,
            strategies: self
                .strategies
                .lock()
                .unwrap()
                .keys()
                .map(|s| s.to_owned())
                .collect(),
            ..Default::default()
        };
        let res = self
            .http
            .post(Registration::endpoint(&self.api_url))
            .body_json(&registration)?
            .await?;
        if !res.status().is_success() {
            return Err(anyhow::anyhow!("Failed to register with unleash API server").into());
        }
        Ok(())
    }

    /// stop the poll_for_updates() function.
    ///
    /// If poll is not running, will wait-loop until poll_for_updates is
    /// running, then signal it to stop, then return. Will wait for ever if
    /// poll_for_updates never starts running.
    pub async fn stop_poll(&self) {
        loop {
            match self
                .polling
                .compare_exchange(true, false, Ordering::Relaxed, Ordering::Relaxed)
            {
                Ok(_) => {
                    return;
                }
                Err(_) => {
                    Delay::new(Duration::from_millis(50)).await;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::hash_map::HashMap;
    use std::collections::hash_set::HashSet;
    use std::hash::BuildHasher;

    use enum_map::Enum;
    use maplit::hashmap;
    use serde::{Deserialize, Serialize};

    use super::ClientBuilder;
    use crate::api::{Feature, Features, Strategy};
    use crate::context::Context;
    use crate::strategy;

    fn features() -> Features {
        Features {
            version: 1,
            features: vec![
                Feature {
                    description: "default".into(),
                    enabled: true,
                    created_at: None,
                    variants: None,
                    name: "default".into(),
                    strategies: vec![Strategy {
                        name: "default".into(),
                        parameters: None,
                    }],
                },
                Feature {
                    description: "userWithId".into(),
                    enabled: true,
                    created_at: None,
                    variants: None,
                    name: "userWithId".into(),
                    strategies: vec![Strategy {
                        name: "userWithId".into(),
                        parameters: Some(hashmap!["userIds".into()=>"present".into()]),
                    }],
                },
                Feature {
                    description: "userWithId+default".into(),
                    enabled: true,
                    created_at: None,
                    variants: None,
                    name: "userWithId+default".into(),
                    strategies: vec![
                        Strategy {
                            name: "userWithId".into(),
                            parameters: Some(hashmap!["userIds".into()=>"present".into()]),
                        },
                        Strategy {
                            name: "default".into(),
                            parameters: None,
                        },
                    ],
                },
                Feature {
                    description: "disabled".into(),
                    enabled: false,
                    created_at: None,
                    variants: None,
                    name: "disabled".into(),
                    strategies: vec![Strategy {
                        name: "default".into(),
                        parameters: None,
                    }],
                },
            ],
        }
    }

    #[test]
    fn test_memoization_enum() {
        let _ = simple_logger::init();
        let f = features();
        // with an enum
        #[allow(non_camel_case_types)]
        #[derive(Debug, Deserialize, Serialize, Enum, Clone)]
        enum UserFeatures {
            unknown,
            default,
            userWithId,
            #[serde(rename = "userWithId+default")]
            userWithId_Default,
            disabled,
        }
        let c = ClientBuilder::default()
            .into_client::<http_client::native::NativeClient, UserFeatures>(
                "http://127.0.0.1:1234/",
                "foo",
                "test",
                None,
            )
            .unwrap();

        c.memoize(f.features).unwrap();
        let present: Context = Context {
            user_id: Some("present".into()),
            ..Default::default()
        };
        let missing: Context = Context {
            user_id: Some("missing".into()),
            ..Default::default()
        };
        // features unknown on the server should honour the default
        assert_eq!(false, c.is_enabled(UserFeatures::unknown, None, false));
        assert_eq!(true, c.is_enabled(UserFeatures::unknown, None, true));
        // default should be enabled, no context needed
        assert_eq!(true, c.is_enabled(UserFeatures::default, None, false));
        // user present should be present on userWithId
        assert_eq!(
            true,
            c.is_enabled(UserFeatures::userWithId, Some(&present), false)
        );
        // user missing should not
        assert_eq!(
            false,
            c.is_enabled(UserFeatures::userWithId, Some(&missing), false)
        );
        // user missing should be present on userWithId+default
        assert_eq!(
            true,
            c.is_enabled(UserFeatures::userWithId_Default, Some(&missing), false)
        );
        // disabled should be disabled
        assert_eq!(false, c.is_enabled(UserFeatures::disabled, None, true));
    }

    #[test]
    fn test_memoization_strs() {
        let _ = simple_logger::init();
        let f = features();
        // And with plain old strings
        #[derive(Debug, Deserialize, Serialize, Enum, Clone)]
        enum NoFeatures {}
        let c = ClientBuilder::default()
            .enable_string_features()
            .into_client::<http_client::native::NativeClient, NoFeatures>(
                "http://127.0.0.1:1234/",
                "foo",
                "test",
                None,
            )
            .unwrap();

        c.memoize(f.features).unwrap();
        let present: Context = Context {
            user_id: Some("present".into()),
            ..Default::default()
        };
        let missing: Context = Context {
            user_id: Some("missing".into()),
            ..Default::default()
        };
        // features unknown on the server should honour the default
        assert_eq!(false, c.is_enabled_str("unknown", None, false));
        assert_eq!(true, c.is_enabled_str("unknown", None, true));
        // default should be enabled, no context needed
        assert_eq!(true, c.is_enabled_str("default", None, false));
        // user present should be present on userWithId
        assert_eq!(true, c.is_enabled_str("userWithId", Some(&present), false));
        // user missing should not
        assert_eq!(false, c.is_enabled_str("userWithId", Some(&missing), false));
        // user missing should be present on userWithId+default
        assert_eq!(
            true,
            c.is_enabled_str("userWithId+default", Some(&missing), false)
        );
        // disabled should be disabled
        assert_eq!(false, c.is_enabled_str("disabled", None, true));
    }

    fn _reversed_uids<S: BuildHasher>(
        parameters: Option<HashMap<String, String, S>>,
    ) -> strategy::Evaluate {
        let mut uids: HashSet<String> = HashSet::new();
        if let Some(parameters) = parameters {
            if let Some(uids_list) = parameters.get("userIds") {
                for uid in uids_list.split(',') {
                    uids.insert(uid.chars().rev().collect());
                }
            }
        }
        Box::new(move |context: &Context| -> bool {
            context
                .user_id
                .as_ref()
                .map(|uid| uids.contains(uid))
                .unwrap_or(false)
        })
    }
    #[test]
    fn test_custom_strategy() {
        let _ = simple_logger::init();
        #[allow(non_camel_case_types)]
        #[derive(Debug, Deserialize, Serialize, Enum, Clone)]
        enum UserFeatures {
            default,
            reversed,
        }
        let client = ClientBuilder::default()
            .strategy("reversed", Box::new(&_reversed_uids))
            .into_client::<http_client::native::NativeClient, UserFeatures>(
                "http://127.0.0.1:1234/",
                "foo",
                "test",
                None,
            )
            .unwrap();

        let f = Features {
            version: 1,
            features: vec![
                Feature {
                    description: "default".into(),
                    enabled: true,
                    created_at: None,
                    variants: None,
                    name: "default".into(),
                    strategies: vec![Strategy {
                        name: "default".into(),
                        parameters: None,
                    }],
                },
                Feature {
                    description: "reversed".into(),
                    enabled: true,
                    created_at: None,
                    variants: None,
                    name: "reversed".into(),
                    strategies: vec![Strategy {
                        name: "reversed".into(),
                        parameters: Some(hashmap!["userIds".into()=>"abc".into()]),
                    }],
                },
            ],
        };
        client.memoize(f.features).unwrap();
        let present: Context = Context {
            user_id: Some("cba".into()),
            ..Default::default()
        };
        let missing: Context = Context {
            user_id: Some("abc".into()),
            ..Default::default()
        };
        // user cba should be present on reversed
        assert_eq!(
            true,
            client.is_enabled(UserFeatures::reversed, Some(&present), false)
        );
        // user abc should not
        assert_eq!(
            false,
            client.is_enabled(UserFeatures::reversed, Some(&missing), false)
        );
        // adding custom strategies shouldn't disable built-in ones
        // default should be enabled, no context needed
        assert_eq!(true, client.is_enabled(UserFeatures::default, None, false));
    }
}