blob: 19640e5ab471e180ff8d7fcedaae2b5e38ee03af [file] [log] [blame]
Matt Rosencrantz19329122015-01-23 22:18:49 -08001// fake implements a fake runtime. The fake runtime is useful in tests when you
2// want to mock out important components.
3// TODO(mattr): Make a more complete, but still fake, implementation.
4package fake
5
6import (
Jiri Simsa6ac95222015-02-23 16:11:49 -08007 "v.io/v23"
8 "v.io/v23/context"
9 "v.io/v23/security"
Matt Rosencrantz19329122015-01-23 22:18:49 -080010
11 tsecurity "v.io/core/veyron/lib/testutil/security"
12)
13
14type contextKey int
15
16const (
17 clientKey = contextKey(iota)
18 principalKey
19 loggerKey
20 backgroundKey
21)
22
23type Runtime struct{}
24
Matt Rosencrantz0a2500b2015-02-26 17:22:17 -080025func new(ctx *context.T) (*Runtime, *context.T, v23.Shutdown, error) {
Matt Rosencrantz19329122015-01-23 22:18:49 -080026 ctx = context.WithValue(ctx, principalKey, tsecurity.NewPrincipal())
27 return &Runtime{}, ctx, func() {}, nil
28}
29
Matt Rosencrantz31995882015-01-27 20:00:48 -080030func (r *Runtime) Init(ctx *context.T) error {
31 return nil
32}
33
Matt Rosencrantz19329122015-01-23 22:18:49 -080034func (r *Runtime) SetPrincipal(ctx *context.T, principal security.Principal) (*context.T, error) {
35 return context.WithValue(ctx, principalKey, principal), nil
36}
37
38func (r *Runtime) GetPrincipal(ctx *context.T) security.Principal {
39 p, _ := ctx.Value(principalKey).(security.Principal)
40 return p
41}
42
Jiri Simsa6ac95222015-02-23 16:11:49 -080043func (r *Runtime) GetAppCycle(ctx *context.T) v23.AppCycle {
Matt Rosencrantz19329122015-01-23 22:18:49 -080044 panic("unimplemented")
45}
46
47func (r *Runtime) SetBackgroundContext(ctx *context.T) *context.T {
48 // Note we add an extra context with a nil value here.
49 // This prevents users from travelling back through the
50 // chain of background contexts.
51 ctx = context.WithValue(ctx, backgroundKey, nil)
52 return context.WithValue(ctx, backgroundKey, ctx)
53}
54
55func (r *Runtime) GetBackgroundContext(ctx *context.T) *context.T {
56 bctx, _ := ctx.Value(backgroundKey).(*context.T)
57 if bctx == nil {
58 // There should always be a background context. If we don't find
59 // it, that means that the user passed us the background context
60 // in hopes of following the chain. Instead we just give them
61 // back what they sent in, which is correct.
62 return ctx
63 }
64 return bctx
65}