github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/mock/context.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package mock is just for test only.
    15  package mock
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/context"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/sessionctx/variable"
    23  )
    24  
    25  var _ context.Context = (*Context)(nil)
    26  
    27  // Context represents mocked context.Context.
    28  type Context struct {
    29  	values map[fmt.Stringer]interface{}
    30  	// mock global variable
    31  	txn   kv.Transaction
    32  	Store kv.Storage
    33  }
    34  
    35  // SetValue implements context.Context SetValue interface.
    36  func (c *Context) SetValue(key fmt.Stringer, value interface{}) {
    37  	c.values[key] = value
    38  }
    39  
    40  // Value implements context.Context Value interface.
    41  func (c *Context) Value(key fmt.Stringer) interface{} {
    42  	value := c.values[key]
    43  	return value
    44  }
    45  
    46  // ClearValue implements context.Context ClearValue interface.
    47  func (c *Context) ClearValue(key fmt.Stringer) {
    48  	delete(c.values, key)
    49  }
    50  
    51  // GetTxn implements context.Context GetTxn interface.
    52  func (c *Context) GetTxn(forceNew bool) (kv.Transaction, error) {
    53  	if c.Store == nil {
    54  		return nil, nil
    55  	}
    56  
    57  	var err error
    58  	if c.txn == nil {
    59  		c.txn, err = c.Store.Begin()
    60  		return c.txn, err
    61  	}
    62  	if forceNew {
    63  		err = c.FinishTxn(false)
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		c.txn, err = c.Store.Begin()
    68  		return c.txn, err
    69  	}
    70  
    71  	return c.txn, nil
    72  }
    73  
    74  // FinishTxn implements context.Context FinishTxn interface.
    75  func (c *Context) FinishTxn(rollback bool) error {
    76  	if c.txn == nil {
    77  		return nil
    78  	}
    79  	defer func() { c.txn = nil }()
    80  
    81  	if rollback {
    82  		return c.txn.Rollback()
    83  	}
    84  
    85  	return c.txn.Commit()
    86  }
    87  
    88  // GetGlobalSysVar implements GlobalVarAccessor GetGlobalSysVar interface.
    89  func (c *Context) GetGlobalSysVar(ctx context.Context, name string) (string, error) {
    90  	v := variable.GetSysVar(name)
    91  	if v == nil {
    92  		return "", variable.UnknownSystemVar.Gen("Unknown system variable: %s", name)
    93  	}
    94  	return v.Value, nil
    95  }
    96  
    97  // SetGlobalSysVar implements GlobalVarAccessor SetGlobalSysVar interface.
    98  func (c *Context) SetGlobalSysVar(ctx context.Context, name string, value string) error {
    99  	v := variable.GetSysVar(name)
   100  	if v == nil {
   101  		return variable.UnknownSystemVar.Gen("Unknown system variable: %s", name)
   102  	}
   103  	v.Value = value
   104  	return nil
   105  }
   106  
   107  // NewContext creates a new mocked context.Context.
   108  func NewContext() *Context {
   109  	return &Context{
   110  		values: make(map[fmt.Stringer]interface{}),
   111  	}
   112  }