text:
import AppBar from 'material-ui/AppBar';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import React, {Component, PropTypes} from 'react';
import spacing from 'material-ui/styles/spacing';
import {connect} from 'react-redux';
import {bindActionCreators} from 'multireducer';
import AppNavDrawer from './AppNavDrawer';
import ChromeHelmet from './ChromeHelmet';
import Footer from './Footer';
import {MainScreenTitle} from './ChromeSettings';
import muiTheme from '../../themes';
import withWidth, {LARGE, MEDIUM} from '../../helpers/withWidth';

import {isLoaded as isInfoLoaded, load as loadInfo} from 'redux/modules/info';
import {isLoaded as isAuthLoaded, load as loadAuth, logout} from 'redux/modules/auth';
import { push } from 'react-router-redux';
import {ReduxAsyncConnect, asyncConnect, reducer as reduxAsyncConnect} from 'redux-async-connect';
import HeaderAvatar from './HeaderAvatar';
import Landing from './Landing';

@asyncConnect([{
	promise: ({store: {dispatch, getState}}) => {
		const promises = [];

		if (!isInfoLoaded(getState())) {
			promises.push(dispatch(loadInfo()));
		}
		if (!isAuthLoaded(getState())) {
			promises.push(dispatch(loadAuth()));
		}

		return Promise.all(promises);
	}
}])
@connect(
	(state, props) => ({user: state.auth.user}),
	(dispatch, props) => bindActionCreators({logout, pushState: push}, dispatch)
)
class Apps extends Component {
	constructor(props, context) {
		super(props, context);

		this.state = {
			navDrawerOpen: false,
		};

		this.muiTheme = getMuiTheme(
			muiTheme, {userAgent: navigator.userAgent}
		);
	}

	static propTypes = {
		children: PropTypes.object.isRequired,
		user: PropTypes.object,
		logout: PropTypes.func.isRequired,
		pushState: PropTypes.func.isRequired,
		width: PropTypes.number,
	};

	static contextTypes = {
		store: PropTypes.object.isRequired,
		router: React.PropTypes.object.isRequired
	};

	static childContextTypes = {
		muiTheme: React.PropTypes.object,
	};

	getChildContext() {
		return ( {
			muiTheme: this.muiTheme
		} );
	}

	_handle_onTouchTap_NavigationToggle = () => {
		this._handle_RequestChangeNavDrawer(!this.state.navDrawerOpen);
	};

	_handle_RequestChangeNavDrawer = (open) => {
		this.setState({
			navDrawerOpen: open,
		})
	};

	_handle_onChangeList_AppNavDrawer = (event, value) => {
		this.context.router.push(value);
		this.setState({
			navDrawerOpen: false,
		});
	};

	getStyles() {
		const styles = {
			appBar: {
				position: 'fixed',
				zIndex: this.muiTheme.zIndex.appBar + 1,
				top: 0,
			},
			root: {
				paddingTop: spacing.desktopKeylineIncrement,
				minHeight: 400,
			},
			content: {
				margin: spacing.desktopGutter,
			},
			contentWhenMedium: {
				margin: `${spacing.desktopGutter * 2}px ${spacing.desktopGutter * 3}px`,
			},
		};

		if (this.props.width === MEDIUM || this.props.width === LARGE)
			styles.content = Object.assign(styles.content, styles.contentWhenMedium);

		return styles;
	}

	componentWillReceiveProps(nextProps) {
		if (!this.props.user && nextProps.user) {
			// login
			this.props.pushState('/home');
		} else if (this.props.user && !nextProps.user) {
			// logout
			this.props.pushState('/');
		}
	}

	handleLogout = (event) => {
		event.preventDefault();
		this.props.logout();
	};

	render() {
		const styles = this.getStyles();

		let {
			navDrawerOpen,
		} = this.state;

		const {
			prepareStyles,
		} = this.muiTheme;

		let docked = false;
		let showMenuIconButton = true;

		if (this.props.width === LARGE) {
			docked = true;
			navDrawerOpen = true;
			showMenuIconButton = false;

			styles.navDrawer = {
				zIndex: styles.appBar.zIndex - 1,
			};
			styles.appBar.paddingLeft = 276;
			styles.root.paddingLeft = 256;
		}

		const {user} = this.props;
		// var user = {foo: 'bar'};
		const homeView = user ?
			(

				<ChromeHelmet />
				<AppBar
				onLeftIconButtonTouchTap={ this._handle_onTouchTap_NavigationToggle }
				title={ MainScreenTitle }
				zDepth={0}
				style={styles.appBar}
				showMenuIconButton={showMenuIconButton}
				iconElementRight={<HeaderAvatar/>}/>

				{ this.props.children }

			<AppNavDrawer
				style={styles.navDrawer}
				location={location}
				docked={docked}
				onRequestChangeNavDrawer={this._handle_RequestChangeNavDrawer}
				onChangeList={ this._handle_onChangeList_AppNavDrawer }
				open={navDrawerOpen}
			/>

)
		: (

				<ChromeHelmet />
				<AppBar
					onLeftIconButtonTouchTap={ this._handle_onTouchTap_NavigationToggle }
					title={ MainScreenTitle }
					zDepth={0}
					style={styles.appBar}
					showMenuIconButton={showMenuIconButton}/>
				{ this.props.children }

		);

		return homeView;

	}
}

//

// Apps.propTypes = {
// 	width: PropTypes.number,
//
// };
//
//
// Apps.childContextTypes = {
// 	muiTheme: React.PropTypes.object,
// };

//

// It is important to retrieve UserToken2, since it is used in client.js
export default withWidth()(Apps);


text:
#ifndef ANCESTORTRANSFORMSCALEDEVENT_H
#define ANCESTORTRANSFORMSCALEDEVENT_H

#include <glm/glm.hpp>

#include <Engine/Event/IEvent.hpp>

namespace Engine
{
	namespace Event
	{
		class AncestorTransformScaledEvent : public IEvent
		{
		public:
			/**
			 * Constructor.
			 *
			 * @param delta The scaling that was applied.
			 */
			AncestorTransformScaledEvent(const glm::vec3& delta);

			/**
			 * Destructor.
			 */
			~AncestorTransformScaledEvent();

			/**
			 * Returns the scaling that was applied to the transform of an
			 * ancestor Game Object.
			 *
			 * @return Scaling that was applied to the transform of an
			 * ancestor Game Object.
			 */
			const glm::vec3& GetDelta() const;

		private:
			/**
			 * The scaling that was applied to the transform of an ancestor
			 * Game Object.
			 */
			glm::vec3 m_delta;
		};
	}
}

#endif

redpajama_set_name: RedPajamaGithub


text:
@interface YR_PersonalPageGalleryDetailViewController : UIViewController

@property (nonatomic, copy) NSString *collection_id;

@end


text:
package com.castlemock.service.mock.rest.project.converter.raml;

import com.castlemock.model.core.http.HttpHeader;
import com.castlemock.model.core.http.HttpMethod;
import com.castlemock.model.mock.rest.domain.RestMethod;
import com.castlemock.model.mock.rest.domain.RestMethodStatus;
import com.castlemock.model.mock.rest.domain.RestMockResponse;
import com.castlemock.model.mock.rest.domain.RestMockResponseStatus;
import com.castlemock.model.mock.rest.domain.RestResource;
import com.castlemock.model.mock.rest.domain.RestResponseStrategy;
import org.raml.v2.api.model.v10.bodies.Response;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
import org.raml.v2.api.model.v10.methods.Method;
import org.raml.v2.api.model.v10.resources.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * The {@link RAML10Parser}
 * @since 1.10
 * @author Karl Dahlgren
 */
class RAML10Parser extends AbstractRAMLParser {

    private static final Logger LOGGER = LoggerFactory.getLogger(RAML10Parser.class);

    public void getResources(final List<Resource> resources, final List<RestResource> result,
                             final String path, final boolean generateResponse){
        if(resources.isEmpty()){
            return;
        }

        for(Resource resource : resources){
            String uri = path + resource.relativeUri().value();

            List<Method> methods = resource.methods();
            if(!methods.isEmpty()){
                RestResource restResource = new RestResource();
                restResource.setName(resource.displayName().value());
                restResource.setUri(uri);
                result.add(restResource);

                for(Method method : methods){
                    HttpMethod httpMethod = HttpMethod.getValue(method.method());
                    if(httpMethod == null){
                        LOGGER.error("The REST method '" + method.method() + "' is not supported.");
                        continue;
                    }

                    RestMethod restMethod = new RestMethod();
                    restMethod.setName(method.displayName().value());
                    restMethod.setStatus(RestMethodStatus.MOCKED);
                    restMethod.setResponseStrategy(RestResponseStrategy.RANDOM);
                    restMethod.setHttpMethod(httpMethod);

                    if(generateResponse){
                        final Collection<RestMockResponse> mockResponses = createMockResponses(method.responses());
                        restMethod.getMockResponses().addAll(mockResponses);
                    }

                    restResource.getMethods().add(restMethod);
                }

            }

            getResources(resource.resources(), result, uri, generateResponse);
        }
    }

    private Collection<RestMockResponse> createMockResponses(List<Response> responses){
        final List<RestMockResponse> mockResponses = new ArrayList<>();

        for(int index = 0; index < responses.size(); index++){
            Response response = responses.get(index);
            String responseCode = response.code().value();
            int httpStatusCode = super.extractHttpStatusCode(responseCode);
            RestMockResponse restMockResponse = new RestMockResponse();
            restMockResponse.setName(RESPONSE_NAME_PREFIX + (index + 1));
            restMockResponse.setHttpStatusCode(httpStatusCode);

            if(httpStatusCode == DEFAULT_RESPONSE_CODE){
                restMockResponse.setStatus(RestMockResponseStatus.ENABLED);
            } else {
                restMockResponse.setStatus(RestMockResponseStatus.DISABLED);
            }

            if(response.body() != null && !response.body().isEmpty()){
                TypeDeclaration typeDeclaration = response.body().get(0);

                if(typeDeclaration.example() != null){
                    String body = typeDeclaration.example().value();
                    restMockResponse.setBody(body);
                }
            }

            if(response.headers() != null){
                for(TypeDeclaration parameter : response.headers()){
                    HttpHeader httpHeader = new HttpHeader();
                    httpHeader.setName(parameter.name());
                    httpHeader.setValue(parameter.defaultValue());
                    restMockResponse.getHttpHeaders().add(httpHeader);
                }
            }

            mockResponses.add(restMockResponse);
        }

        return mockResponses;
    }

}


text:
package mysqlctl

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"sync"

	log "github.com/golang/glog"
	"golang.org/x/net/context"

	"github.com/youtube/vitess/go/mysql"
	"github.com/youtube/vitess/go/pools"
	"github.com/youtube/vitess/go/sqldb"
	"github.com/youtube/vitess/go/vt/dbconfigs"
	"github.com/youtube/vitess/go/vt/mysqlctl/replication"
)

var (
	// ErrBinlogUnavailable is returned by this library when we
	// cannot find a suitable binlog to satisfy the request.
	ErrBinlogUnavailable = fmt.Errorf("cannot find relevant binlogs on this server")
)

// SlaveConnection represents a connection to mysqld that pretends to be a slave
// connecting for replication. Each such connection must identify itself to
// mysqld with a server ID that is unique both among other SlaveConnections and
// among actual slaves in the topology.
type SlaveConnection struct {
	sqldb.Conn
	mysqld  *Mysqld
	slaveID uint32
	cancel  context.CancelFunc
	wg      sync.WaitGroup
}

// NewSlaveConnection creates a new slave connection to the mysqld instance.
// It uses a pools.IDPool to ensure that the server IDs used to connect are
// unique within this process. This is done with the assumptions that:
//
// 1) No other processes are making fake slave connections to our mysqld.
// 2) No real slave servers will have IDs in the range 1-N where N is the peak
//    number of concurrent fake slave connections we will ever make.
func (mysqld *Mysqld) NewSlaveConnection() (*SlaveConnection, error) {
	conn, err := mysqld.connectForReplication()
	if err != nil {
		return nil, err
	}

	sc := &SlaveConnection{
		Conn:    conn,
		mysqld:  mysqld,
		slaveID: slaveIDPool.Get(),
	}
	log.Infof("new slave connection: slaveID=%d", sc.slaveID)
	return sc, nil
}

// connectForReplication create a MySQL connection ready to use for replication.
func (mysqld *Mysqld) connectForReplication() (sqldb.Conn, error) {
	params, err := dbconfigs.MysqlParams(mysqld.dba)
	if err != nil {
		return nil, err
	}

	conn, err := sqldb.Connect(params)
	if err != nil {
		return nil, err
	}

	// Tell the server that we understand the format of events
	// that will be used if binlog_checksum is enabled on the server.
	if _, err := conn.ExecuteFetch("SET @master_binlog_checksum=@@global.binlog_checksum", 0, false); err != nil {
		return nil, fmt.Errorf("failed to set @master_binlog_checksum=@@global.binlog_checksum: %v", err)
	}

	return conn, nil
}

// slaveIDPool is the IDPool for server IDs used to connect as a slave.
var slaveIDPool = pools.NewIDPool()

// StartBinlogDumpFromCurrent requests a replication binlog dump from
// the current position.
func (sc *SlaveConnection) StartBinlogDumpFromCurrent(ctx context.Context) (replication.Position, <-chan replication.BinlogEvent, error) {
	ctx, sc.cancel = context.WithCancel(ctx)

	flavor, err := sc.mysqld.flavor()
	if err != nil {
		return replication.Position{}, nil, fmt.Errorf("StartBinlogDump needs flavor: %v", err)
	}

	masterPosition, err := flavor.MasterPosition(sc.mysqld)
	if err != nil {
		return replication.Position{}, nil, fmt.Errorf("failed to get master position: %v", err)
	}

	c, err := sc.StartBinlogDumpFromPosition(ctx, masterPosition)
	return masterPosition, c, err
}

// StartBinlogDumpFromPosition requests a replication binlog dump from
// the master mysqld at the given Position and then sends binlog
// events to the provided channel.
// The stream will continue in the background, waiting for new events if
// necessary, until the connection is closed, either by the master or
// by canceling the context.
//
// Note the context is valid and used until eventChan is closed.
func (sc *SlaveConnection) StartBinlogDumpFromPosition(ctx context.Context, startPos replication.Position) (<-chan replication.BinlogEvent, error) {
	ctx, sc.cancel = context.WithCancel(ctx)

	flavor, err := sc.mysqld.flavor()
	if err != nil {
		return nil, fmt.Errorf("StartBinlogDump needs flavor: %v", err)
	}

	log.Infof("sending binlog dump command: startPos=%v, slaveID=%v", startPos, sc.slaveID)
	if err = flavor.SendBinlogDumpCommand(sc, startPos); err != nil {
		log.Errorf("couldn't send binlog dump command: %v", err)
		return nil, err
	}

	// Read the first packet to see if it's an error response to our dump command.
	buf, err := sc.Conn.ReadPacket()
	if err != nil {
		log.Errorf("couldn't start binlog dump: %v", err)
		return nil, err
	}

	// FIXME(alainjobart) I think we can use a buffered channel for better performance.
	eventChan := make(chan replication.BinlogEvent)

	// Start reading events.
	sc.wg.Add(1)
	go func() {
		defer func() {
			close(eventChan)
			sc.wg.Done()
		}()
		for {
			if buf[0] == 254 {
				// The master is telling us to stop.
				log.Infof("received EOF packet in binlog dump: %#v", buf)
				return
			}

			select {
			// Skip the first byte because it's only used for signaling EOF.
			case eventChan <- flavor.MakeBinlogEvent(buf[1:]):
			case <-ctx.Done():
				return
			}

			buf, err = sc.Conn.ReadPacket()
			if err != nil {
				if sqlErr, ok := err.(*sqldb.SQLError); ok && sqlErr.Number() == mysql.ErrServerLost {
					// ErrServerLost = Lost connection to MySQL server during query
					// This is not necessarily an error. It could just be that we closed
					// the connection from outside.
					log.Infof("connection closed during binlog stream (possibly intentional): %v", err)
					return
				}
				log.Errorf("read error while streaming binlog events: %v", err)
				return
			}
		}
	}()

	return eventChan, nil
}

// StartBinlogDumpFromBinlogBeforeTimestamp requests a replication
// binlog dump from the master mysqld starting with a file that has
// timestamps smaller than the provided timestamp, and then sends
// binlog events to the provided channel.
//
// The startup phase will list all the binary logs, and find the one
// that has events starting strictly before the provided timestamp. It
// will then start from there, and stream all events. It is the
// responsability of the calling site to filter the events more.
//
// MySQL 5.6+ note: we need to do it that way because of the way the
// GTIDSet works. In the previous two streaming functions, we pass in
// the full GTIDSet (that has the list of all transactions seen in
// the replication stream). In this case, we don't know it, all we
// have is the binlog file names. We depend on parsing the first
// PREVIOUS_GTIDS_EVENT event in the logs to get it. So we need the
// caller to parse that event, and it can't be skipped because its
// timestamp is lower. Then, for each subsequent event, the caller
// also needs to add the event GTID to its GTIDSet. Otherwise it won't
// be correct ever. So the caller really needs to build up its GTIDSet
// along the entire file, not just for events whose timestamp is in a
// given range.
//
// The stream will continue in the background, waiting for new events if
// necessary, until the connection is closed, either by the master or
// by canceling the context.
//
// Note the context is valid and used until eventChan is closed.
func (sc *SlaveConnection) StartBinlogDumpFromBinlogBeforeTimestamp(ctx context.Context, timestamp int64) (<-chan replication.BinlogEvent, error) {
	ctx, sc.cancel = context.WithCancel(ctx)

	flavor, err := sc.mysqld.flavor()
	if err != nil {
		return nil, fmt.Errorf("StartBinlogDump needs flavor: %v", err)
	}

	// List the binlogs.
	binlogs, err := sc.Conn.ExecuteFetch("SHOW BINARY LOGS", 1000, false)
	if err != nil {
		return nil, fmt.Errorf("failed to SHOW BINARY LOGS: %v", err)
	}

	// Start with the most recent binlog file until we find the right event.
	var binlogIndex int
	var event replication.BinlogEvent
	for binlogIndex = len(binlogs.Rows) - 1; binlogIndex >= 0; binlogIndex-- {
		// Exit the loop early if context is canceled.
		select {
		case <-ctx.Done():
			return nil, ctx.Err()
		default:
		}

		// Start dumping the logs. The position is '4' to skip the
		// Binlog File Header. See this page for more info:
		// https://dev.mysql.com/doc/internals/en/binlog-file.html
		binlog := binlogs.Rows[binlogIndex][0].String()
		cmd := makeBinlogDumpCommand(4, 0, sc.slaveID, binlog)
		if err := sc.Conn.SendCommand(ComBinlogDump, cmd); err != nil {
			return nil, fmt.Errorf("failed to send the ComBinlogDump command: %v", err)
		}

		// Get the first event to get its timestamp. We skip
		// events that don't have timestamps (although it seems
		// most do anyway).
		for {
			buf, err := sc.Conn.ReadPacket()
			if err != nil {
				return nil, fmt.Errorf("couldn't start binlog dump of binlog %v: %v", binlog, err)
			}

			// Why would the master tell us to stop here?
			if buf[0] == 254 {
				return nil, fmt.Errorf("received EOF packet for first packet of binlog %v", binlog)
			}

			// Parse the full event.
			event = flavor.MakeBinlogEvent(buf[1:])
			if !event.IsValid() {
				return nil, fmt.Errorf("first event from binlog %v is not valid", binlog)
			}
			if event.Timestamp() > 0 {
				// We found the first event with a
				// valid timestamp.
				break
			}
		}
		if int64(event.Timestamp()) < timestamp {
			// The first event in this binlog has a smaller
			// timestamp than what we need, we found a good
			// starting point.
			break
		}

		// The timestamp is higher, we need to try the older files.
		// Close and re-open our connection.
		sc.Conn.Close()
		conn, err := sc.mysqld.connectForReplication()
		if err != nil {
			return nil, err
		}
		sc.Conn = conn
	}
	if binlogIndex == -1 {
		// We haven't found a suitable binlog
		log.Errorf("couldn't find an old enough binlog to match timestamp >= %v (looked at %v files)", timestamp, len(binlogs.Rows))
		return nil, ErrBinlogUnavailable
	}

	// Now just loop sending and reading events.
	// FIXME(alainjobart) I think we can use a buffered channel for better performance.
	eventChan := make(chan replication.BinlogEvent)

	// Start reading events.
	sc.wg.Add(1)
	go func() {
		defer func() {
			close(eventChan)
			sc.wg.Done()
		}()

		for {
			select {
			case eventChan <- event:
			case <-ctx.Done():
				return
			}

			buf, err := sc.Conn.ReadPacket()
			if err != nil {
				if sqlErr, ok := err.(*sqldb.SQLError); ok && sqlErr.Number() == mysql.ErrServerLost {
					// ErrServerLost = Lost connection to MySQL server during query
					// This is not necessarily an error. It could just be that we closed
					// the connection from outside.
					log.Infof("connection closed during binlog stream (possibly intentional): %v", err)
					return
				}
				log.Errorf("read error while streaming binlog events: %v", err)
				return
			}

			if buf[0] == 254 {
				// The master is telling us to stop.
				log.Infof("received EOF packet in binlog dump: %#v", buf)
				return
			}

			// Skip the first byte because it's only used
			// for signaling EOF.
			event = flavor.MakeBinlogEvent(buf[1:])
		}
	}()

	return eventChan, nil
}

// Close closes the slave connection, which also signals an ongoing dump
// started with StartBinlogDump() to stop and close its BinlogEvent channel.
// The ID for the slave connection is recycled back into the pool.
func (sc *SlaveConnection) Close() {
	if sc.Conn != nil {
		log.Infof("shutting down slave socket to unblock reads")
		sc.Conn.Shutdown()

		log.Infof("waiting for slave dump thread to end")
		sc.cancel()
		sc.wg.Wait()

		log.Infof("closing slave MySQL client, recycling slaveID %v", sc.slaveID)
		sc.Conn.Close()
		sc.Conn = nil
		slaveIDPool.Put(sc.slaveID)
	}
}

// makeBinlogDumpCommand builds a buffer containing the data for a MySQL
// COM_BINLOG_DUMP command.
func makeBinlogDumpCommand(pos uint32, flags uint16, serverID uint32, filename string) []byte {
	var buf bytes.Buffer
	buf.Grow(4 + 2 + 4 + len(filename))

	// binlog_pos (4 bytes)
	binary.Write(&buf, binary.LittleEndian, pos)
	// binlog_flags (2 bytes)
	binary.Write(&buf, binary.LittleEndian, flags)
	// server_id of slave (4 bytes)
	binary.Write(&buf, binary.LittleEndian, serverID)
	// binlog_filename (string with no terminator and no length)
	buf.WriteString(filename)

	return buf.Bytes()
}

// ComBinlogDump is the command id for COM_BINLOG_DUMP.
const ComBinlogDump = 0x12


text:
package mixcoin

import (
	"encoding/json"
	"github.com/stretchr/testify/assert"
	"log"
	"sort"
	"testing"
)

type Pair struct {
	key string
	val string
}

func (m *Pair) Key() string {
	return m.key
}

func (m *Pair) Serialize() []byte {
	serialized, err := json.Marshal(m)
	if err != nil {
		log.Panicf("error serializing pair: %v", err)
	}
	return serialized
}

var (
	items = []*Pair{
		&Pair{"gorilla", "primate"},
		&Pair{"giraffe", "long-necked animal"},
		&Pair{"bat", "mammal"},
		&Pair{"jellyfish", "cnidarian"},
		&Pair{"shark", "fish"},
	}
)

func TestRandomizingPool(t *testing.T) {
	p := NewRandomizingPool()
	keys := make(map[string]int)
	for _, item := range items {
		keys[item.Key()] = 0
		p.Put(item)
	}

	for i := 0; i < len(items); i++ {
		item, err := p.Get()
		assert.Nil(t, err, "there should be items remaining")
		count, ok := keys[item.Key()]
		assert.True(t, ok, "key should be present")
		assert.Equal(t, count, 0, "key shouldn't have been seen yet")

		keys[item.Key()] += 1
	}

	_, err := p.Get()
	assert.NotNil(t, err, "pool should empty now")
}

func TestReceivingPool(t *testing.T) {
	p := NewReceivingPool()

	var keys []string
	for _, item := range items {
		p.Put(item)
		keys = append(keys, item.Key())
	}
	gotKeys := p.Keys()
	log.Printf("got keys: %v", gotKeys)
	log.Printf("want keys: %v", keys)
	assert.True(t, setEquals(keys, gotKeys), "pool.Keys should return correct slice of keys")

	gotKeys = nil
	subset := []string{"giraffe", "jellyfish", "shark"}
	for _, item := range p.Scan(subset) {
		gotKeys = append(gotKeys, item.Key())
	}
	log.Printf("got keys: %v", gotKeys)
	log.Printf("want keys: %v", subset)
	assert.True(t, setEquals(subset, gotKeys), "pool.Scan should return correct intersection")
}

func setEquals(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}

	sort.Strings(a)
	sort.Strings(b)

	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}


text:
This subdirectories saved here are STADIC projects which can be used to test the visulization software.
There is a readme.md file inside each subdirectory that mentions the location where the subdirectory should
be stored on the disc.


text:
FROM python:3.6.2-slim

RUN apt-get update && \
    apt-get install -y git make gcc libssl-dev libgmp-dev python-dev libxml2-dev libxslt1-dev zlib1g-dev

ADD ./requirements.txt /
RUN pip3 install -r requirements.txt

WORKDIR /src/

CMD ["python", "/src/main.py"]
EXPOSE 5000


text:
namespace KISSMp3MoverBefore
{
    using System;
    using System.IO;
    using Contracts;
    public class Mp3Mover : IMp3Mover
    {
        private static IMp3Mover mp3MoverInstance;

        private Mp3Mover()
        {
        }

        public static IMp3Mover Instance => mp3MoverInstance ?? (mp3MoverInstance = new Mp3Mover());

        public string DirectoryPath { get; set; }

        public IFileSelectStrategy SelectStrategy { get; set; }

        public IFileRenameStrategy RenameStrategy { get; set; }

        public IFileMoveStrategy MoveStrategy { get; set; }

        public void Start()
        {
            foreach (var fileName in Directory.EnumerateFiles(this.DirectoryPath))
            {
                if (this.SelectStrategy.CanBeSelected(fileName))
                {
                    this.RenameStrategy.Rename(fileName);
                    var artist = fileName.Substring(0, fileName.IndexOf(" - ", StringComparison.Ordinal));
                    this.MoveStrategy.Move(fileName, artist + "/" + fileName);
                }
            }
        }
    }
}


text:
'use strict';

import Camera from './camera';
import Recognizer from './recognizer';
import Authenticator from './authenticator';
import Gate from './gate';

export default () => {
  const camera = new Camera;
  const recognizer = new Recognizer;
  const gate = new Gate;
  const authenticator = new Authenticator;

  camera.on('image', image => recognizer.enqueue(image));

  recognizer
    .on('plates', (plates) => {
      authenticator
        .check(plates)
        .then(() => gate.open());
    })
    .start();

  camera.start();
};


text:
from cinder.api import common

class ViewBuilder(common.ViewBuilder):
    """Model capabilities API responses as a python dictionary."""

    _collection_name = "capabilities"

    def __init__(self):
        """Initialize view builder."""
        super(ViewBuilder, self).__init__()

    def summary(self, request, capabilities, id):
        """Summary view of a backend capabilities."""
        return {
            'namespace': 'OS::Storage::Capabilities::%s' % id,
            'vendor_name': capabilities.get('vendor_name'),
            'volume_backend_name': capabilities.get('volume_backend_name'),
            'pool_name': capabilities.get('pool_name'),
            'driver_version': capabilities.get('driver_version'),
            'storage_protocol': capabilities.get('storage_protocol'),
            'display_name': capabilities.get('display_name'),
            'description': capabilities.get('description'),
            'visibility': capabilities.get('visibility'),
            'replication_targets': capabilities.get('replication_targets', []),
            'properties': capabilities.get('properties'),
        }


text:
var base_output = require('../lib/base_output'),
  util = require('util'),
  logger = require('log4node'),
  error_buffer = require('../lib/error_buffer'),
  ssl_helper = require('../lib/ssl_helper'),
  lumberjack = require('lumberjack-protocol');

function OutputLumberJack() {
  base_output.BaseOutput.call(this);
  this.mergeConfig(ssl_helper.config());
  this.mergeConfig(error_buffer.config(function() {
    return 'lumber jack to ' + this.host + ':' + this.port;
  }));
  this.mergeConfig({
    name: 'Lumber Jack',
    host_field: 'host',
    port_field: 'port',
    optional_params: ['crt', 'key', 'ca', 'max_queue_size'],
    default_values: {
      max_queue_size: 500,
    },
    start_hook: this.start,
  });
}

util.inherits(OutputLumberJack, base_output.BaseOutput);

OutputLumberJack.prototype.start = function(callback) {
  logger.info('Creating LumberJack output to', this.host + ':' + this.port, 'using ca', this.ca);
  var options = {
    maxQueueSize: this.max_queue_size,
  };
  var tls_options = ssl_helper.merge_options(this, {
    host: this.host,
    port: this.port,
  });
  this.client = lumberjack.client(tls_options, options);
  this.client.on('dropped', function(count) {
    this.error_buffer.emit('error', new Error('Dropping data, queue size :' + count));
  }.bind(this));
  callback();
};

OutputLumberJack.prototype.process = function(data) {
  if (this.client) {
    this.client.writeDataFrame(data, function() {
      this.error_buffer.emit('ok');
    }.bind(this));
  }
};

OutputLumberJack.prototype.close = function(callback) {
  logger.info('Closing LumberJack output to', this.host + ':' + this.port);
  if (this.client) {
    this.client.close();
  }
  callback();
};

exports.create = function() {
  return new OutputLumberJack();
};


text:
package types

import (
    "reflect"
    "fmt"
    "sort"
    "mingle/parser"
//    "log"
    "bitgirder/assert"
    mg "mingle"
)

var (
    mkQn = parser.MustQualifiedTypeName
    mkId = parser.MustIdentifier
    mkNs = parser.MustNamespace
    mkTyp = parser.MustTypeReference
    asType = parser.AsTypeReference
)

func idSetFor( m *mg.IdentifierMap ) []*mg.Identifier {
    res := make( []*mg.Identifier, 0, m.Len() )
    m.EachPair( func( id *mg.Identifier, _ interface{} ) {
        res = append( res, id )
    })
    return res
}

func MakeFieldDef(
    nm string, typ interface{}, defl interface{} ) *FieldDefinition {

    res := &FieldDefinition{ Name: mkId( nm ), Type: asType( typ ) }
    if defl != nil {
        if val, err := mg.AsValue( defl ); err == nil {
            res.Default = val
        } else { panic( err ) }
    }
    return res
}

func buildFieldSet( fs *FieldSet, flds []*FieldDefinition ) {
    for _, fld := range flds { fs.MustAdd( fld ) }
}

func MakeFieldSet( flds ...*FieldDefinition ) *FieldSet {
    res := NewFieldSet()
    buildFieldSet( res, flds )
    return res
}

func MakeStructDef( qn string, flds []*FieldDefinition ) *StructDefinition {
    if flds == nil { flds = []*FieldDefinition{} }
    res := NewStructDefinition()
    res.Name = parser.MustQualifiedTypeName( qn )
    buildFieldSet( res.Fields, flds )
    return res
}

func MakeSchemaDef( qn string, flds []*FieldDefinition ) *SchemaDefinition {
    res := NewSchemaDefinition()
    res.Name = mkQn( qn )
    buildFieldSet( res.Fields, flds )
    return res
}

func MakeEnumDef( qn string, vals ...string ) *EnumDefinition {
    res := &EnumDefinition{
        Name: parser.MustQualifiedTypeName( qn ),
        Values: make( []*mg.Identifier, len( vals ) ),
    }
    for i, val := range vals { res.Values[ i ] = parser.MustIdentifier( val ) }
    return res
}

func MakeUnionDef( qn string, typs ...interface{} ) *UnionDefinition {
    mgTyps := make( []mg.TypeReference, len( typs ) )
    for i, typ := range typs { mgTyps[ i ] = asType( typ ) }
    return &UnionDefinition{
        Name: mkQn( qn ),
        Union: MustUnionTypeDefinitionTypes( mgTyps... ),
    }
}

func MakeCallSig(
    flds []*FieldDefinition, retType string, throws []string ) *CallSignature {

    res := NewCallSignature()
    for _, fld := range flds { res.Fields.MustAdd( fld ) }
    res.Return = mkTyp( retType )
    throwsTyps := make( []mg.TypeReference, len( throws ) )
    for i, typ := range throws { throwsTyps[ i ] = mkTyp( typ ) }
    if len( throwsTyps ) > 0 {
        res.Throws = MustUnionTypeDefinitionTypes( throwsTyps... )
    }
    return res
}

func MakeOpDef( nm string, sig *CallSignature ) *OperationDefinition {
    return &OperationDefinition{ Name: mkId( nm ), Signature: sig }
}

func MakeServiceDef(
    qn, secQn string, opDefs ...*OperationDefinition ) *ServiceDefinition {
    res := NewServiceDefinition()
    res.Name = parser.MustQualifiedTypeName( qn )
    res.Operations = append( res.Operations, opDefs... )
    if secQn != "" { res.Security = parser.MustQualifiedTypeName( secQn ) }
    return res
}

func mustAddDefs( dm *DefinitionMap, defs []Definition ) *DefinitionMap {
    for _, d := range defs { dm.MustAdd( d ) }
    return dm
}

func MakeDefMap( defs ...Definition ) *DefinitionMap {
    return mustAddDefs( NewDefinitionMap(), defs )
}

type DefAsserter struct {
    *assert.PathAsserter
}

func NewDefAsserter( f assert.Failer ) *DefAsserter {
    return &DefAsserter{ assert.NewPathAsserter( f ) }
}

func ( a *DefAsserter ) descend( node interface{} ) *DefAsserter {
    return &DefAsserter{ a.PathAsserter.Descend( node ) }
}

func ( a *DefAsserter ) startList() *DefAsserter {
    return &DefAsserter{ a.PathAsserter.StartList() }
}

func ( a *DefAsserter ) next() *DefAsserter {
    return &DefAsserter{ a.PathAsserter.Next() }
}

func ( a *DefAsserter ) equalType( v1, v2 interface{} ) interface{} {
    t1, t2 := reflect.TypeOf( v1 ), reflect.TypeOf( v2 )
    if t1 != t2 { a.Fatalf( "Expected %T but got %T", v1, v2 ) }
    return v2
}

func ( a *DefAsserter ) equalTypeRef( t1, t2 mg.TypeReference ) {
    a.Truef( t1.Equals( t2 ), "%s != %s", t1, t2 )
}

func ( a *DefAsserter ) assertPrimDef(
    p1 *PrimitiveDefinition, d2 Definition ) {
    _ = a.equalType( p1, d2 ).( *PrimitiveDefinition )
}

func ( a *DefAsserter ) assertAliasDef(
    a1 *AliasedTypeDefinition, d2 Definition ) {

    a2 := a.equalType( a1, d2 ).( *AliasedTypeDefinition )
    a.Descend( "Name" ).Equal( a1.Name, a2.Name )
    a.Descend( "AliasedType" ).Equal( a1.AliasedType, a2.AliasedType )
}

func asCompStr( ids []*mg.Identifier ) string {
    strs := make( []string, len( ids ) )
    for i, id := range ids { strs[ i ] = id.ExternalForm() }
    sort.Strings( strs )
    return fmt.Sprintf( "%v", strs )
}

func ( a *DefAsserter ) assertIdSets( ids1, ids2 []*mg.Identifier ) {
    if cs1, cs2 := asCompStr( ids1 ), asCompStr( ids2 ); cs1 != cs2 {
        a.Fatalf( "Id sets differ: %s != %s", cs1, cs2 )
    }
}

func ( a *DefAsserter ) assertFieldDef( fd1, fd2 *FieldDefinition ) {
    a.descend( "(Name)" ).Equal( fd1.Name, fd2.Name )
    a.descend( "(Type)" ).equalTypeRef( fd1.Type, fd2.Type )
    mg.AssertEqualValues( fd1.Default, fd2.Default, a.descend( "(Default)" ) )
}

// First check that both have same field sets, then check field by field
func ( a *DefAsserter ) assertFieldSets( fs1, fs2 *FieldSet ) {
    a.assertIdSets( fs1.GetFieldNames(), fs2.GetFieldNames() )
    fs1.EachDefinition( func( fd1 *FieldDefinition ) {
        fd2 := fs2.Get( fd1.Name )
        a.descend( fd1.Name ).assertFieldDef( fd1, fd2 )
    })
}

func ( a *DefAsserter ) assertUnionType( ut1, ut2 *UnionTypeDefinition ) {
    if ut1 == nil && ut2 == nil { return }
    if ut1 == nil { a.Truef( ut2 == nil, "ut2 not nil" ) }
    if ut2 == nil { a.Truef( ut1 == nil, "ut1 not nil" ) }
    typs1, typs2 := ut1.Types, ut2.Types
    a.descend( "(Len)" ).Equal( len( typs1 ), len( typs2 ) )
    la := a.startList()
    for i, e := 0, len( typs1 ); i < e; i++ {
        la.descend( "(Type)" ).Equal( typs1[ i ], typs2[ i ] )
        la = la.next()
    }
}

func ( a *DefAsserter ) assertUnionDef( ud1 *UnionDefinition, d2 Definition ) {
    ud2 := a.equalType( ud1, d2 ).( *UnionDefinition )
    a.descend( "(Name)" ).Equal( ud1.Name, ud2.Name )
    a.descend( "(Union)" ).assertUnionType( ud1.Union, ud2.Union )
}

func ( a *DefAsserter ) assertStructDef(
    s1 *StructDefinition, d2 Definition ) {

    s2 := a.equalType( s1, d2 ).( *StructDefinition )
    a.descend( "(Fields)" ).assertFieldSets( s1.Fields, s2.Fields )
    a.descend( "(Constructors)" ).
        assertUnionType( s1.Constructors, s2.Constructors )
}

func ( a *DefAsserter ) assertSchemaDef( s1 *SchemaDefinition, d2 Definition ) {
    s2 := a.equalType( s1, d2 ).( *SchemaDefinition )
    a.descend( "(Fields)" ).assertFieldSets( s1.Fields, s2.Fields )
}

func ( a *DefAsserter ) assertEnumDef(
    e1 *EnumDefinition, v2 interface{} ) {
    e2 := a.equalType( e1, v2 ).( *EnumDefinition )
    a.descend( "(Values)" ).assertIdSets( e1.Values, e2.Values )
}

func ( a *DefAsserter ) assertCallSig( s1, s2 *CallSignature ) {
    a.descend( "(Fields)" ).assertFieldSets( s1.Fields, s2.Fields )
    a.descend( "(Return)" ).Equal( s1.Return, s2.Return )
    a.descend( "(Throws)" ).assertUnionType( s1.Throws, s2.Throws )
}

func ( a *DefAsserter ) assertProtoDef(
    p1 *PrototypeDefinition, v2 interface{} ) {
    p2 := a.equalType( p1, v2 ).( *PrototypeDefinition )
    a.descend( "Signature" ).assertCallSig( p1.Signature, p2.Signature )
}

func ( a *DefAsserter ) assertOpDef( od1, od2 *OperationDefinition ) {
    a.descend( "(Name)" ).Equal( od1.Name, od2.Name )
    a.descend( "(Signature" ).assertCallSig( od1.Signature, od2.Signature )
}

func ( a *DefAsserter ) assertOpDefs(
    defs1, defs2 []*OperationDefinition ) {
    m1, m2 := OpDefsByName( defs1 ), OpDefsByName( defs2 )
    a.descend( "(Len)" ).Equal( m1.Len(), m2.Len() )
    a.descend( "(OpNames)" ).assertIdSets( idSetFor( m1 ), idSetFor( m2 ) )
    m1.EachPair( func( id *mg.Identifier, val interface{} ) {
        opDef1 := val.( *OperationDefinition )
        opDef2, _ := m2.Get( id ).( *OperationDefinition )
        a.descend( id.ExternalForm() ).assertOpDef( opDef1, opDef2 )
    })
}

func ( a *DefAsserter ) assertServiceDef(
    s1 *ServiceDefinition, v2 interface{} ) {

    s2 := a.equalType( s1, v2 ).( *ServiceDefinition )
    a.descend( "(Operations)" ).assertOpDefs( s1.Operations, s2.Operations )
    a.descend( "(Security)" ).Equal( s1.Security, s2.Security )
}

func ( a *DefAsserter ) AssertDef( d1, d2 Definition ) {
    a.descend( "(Name)" ).Equal( d1.GetName(), d2.GetName() )
    a = a.descend( d1.GetName() )
    switch v := d1.( type ) {
    case *PrimitiveDefinition: a.assertPrimDef( v, d2 )
    case *AliasedTypeDefinition: a.assertAliasDef( v, d2 )
    case *StructDefinition: a.assertStructDef( v, d2 )
    case *SchemaDefinition: a.assertSchemaDef( v, d2 )
    case *EnumDefinition: a.assertEnumDef( v, d2 )
    case *UnionDefinition: a.assertUnionDef( v, d2 )
    case *PrototypeDefinition: a.assertProtoDef( v, d2 )
    case *ServiceDefinition: a.assertServiceDef( v, d2 )
    default: a.Fatalf( "Unhandled def: %T", d1 )
    }
}

func ( a *DefAsserter ) AssertDefMaps( m1, m2 *DefinitionMap ) {
    a.descend( "(Len)" ).Equal( m1.Len(), m2.Len() )
    m1.EachDefinition( func( d1 Definition ) {
        nm := d1.GetName()
        da := a.descend( nm.ExternalForm() )
        if d2 := m2.Get( nm ); d2 == nil {
            da.Fatal( "No corresponding entry in m2" )
        } else { da.AssertDef( d1, d2 ) }
    })
}


text:
/**
 * @description bold test
 * @author wangfupeng
 */

import createEditor from '../../helpers/create-editor'
import Editor from '../../../src/editor'
import Bold from '../../../src/menus/bold/index'
import mockCmdFn from '../../helpers/command-mock'
import { getMenuInstance } from '../../helpers/menus'

let editor: Editor
let boldMenu: Bold

test('加粗', () => {
    editor = createEditor(document, 'div1') // 赋值给全局变量

    // 找到 bold 菜单
    boldMenu = getMenuInstance(editor, Bold) as Bold

    // 执行点击事件,模拟加粗
    mockCmdFn(document)
    ;(boldMenu as Bold).clickHandler()
    expect(document.execCommand).toBeCalledWith('bold', false, undefined) // mock fn 被调用
})


text:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appacitive.Sdk.Tests
{
    [TestClass]
    public class RealTimeFixture
    {

    }
}


text:
require "uri"
require "dhis2"

# DHIS2 helper class
class DHIS2Helper
  attr_reader :url, :client

  def initialize(uri)
    uri = URI.parse(uri)
    @url = "#{uri.scheme}://#{uri.host}:#{uri.port}#{uri.path}"
    @client = Dhis2::Client.new(url:      url,
                                user:     uri.user,
                                password: uri.password,
                                debug:    ENV["DEBUG"] == "true")
  end

  def max_level
    levels.size
  end

  def find_group_by_name(name)
    @client.organisation_unit_groups.find_by(name: name)
  end

  def add_to_group(group, org_units)
    org_units.each_with_index do |ou_id, index|
      puts "Adding organisation unit #{ou_id} (#{index + 1}/#{org_units.size})"
      begin
        group.add_relation("organisationUnits", ou_id)
      rescue
        puts "Organisation unit #{ou_id} not found, passing"
      end
    end

    find_group_by_name(group.name)
  end

  def find_or_create_group(name)
    group = @client.organisation_unit_groups.find_by(name: name)

    if group
      puts "Existing group: #{group.name} with #{group.organisation_units.size} units will be updated"
    else
      puts "Group #{name} does not exist, creating"

      if create_group(name)
        group = @client.organisation_unit_groups.find_by(name: name)
        puts "Group #{name} created."
      else
        puts "Unable to create group #{name}. Exiting.".red
        exit
      end
    end

    group
  end

  def create_group(name)
    org_unit_groups = { name: name, short_name: name[0..20] }
    @client.organisation_unit_groups.create(org_unit_groups).success?
  end

  def levels
    @client.organisation_unit_levels.list
  end

  def org_units(level)
    @client.organisation_units.list(filter:    "level:eq:#{level}",
                                    fields:    %w[id displayName shortName
                                                  openingDate parent],
                                    page_size: 100_000)
  end

  def create_org_unit(org_units)
    payload = org_units.map do |org_unit|
      { id:           org_unit.id,
        name:         org_unit.display_name,
        short_name:   org_unit.short_name,
        opening_date: org_unit.opening_date,
        parent_id:    org_unit.parent_id }
    end
    @client.organisation_units.create(payload)
  end
end


text:
package com.opensymphony.workflow.spi;

import java.util.Date;

/**
 * Interface for a step associated with a workflow instance.
 *
 * @author Pat Lightbody
 */
public interface Step {
    //~ Methods ////////////////////////////////////////////////////////////////

    /**
     * Returns the ID of the action associated with this step,
     * or 0 if there is no action associated.
     */
    public int getActionId();

    public String getCaller();

    /**
     * Returns an optional date signifying when this step must be finished.
     */
    public Date getDueDate();

    /**
     * Returns the unique ID of the workflow entry.
     */
    public long getEntryId();

    /**
     * Returns the date this step was finished, or null if it isn't finished.
     */
    public Date getFinishDate();

    /**
     * Returns the unique ID of this step.
     */
    public long getId();

    /**
     * Returns the owner of this step, or null if there is no owner.
     */
    public String getOwner();

    /**
     * Returns the unique ID of the previous step, or 0 if this is the first step.
     */
    public long[] getPreviousStepIds();

    /**
     * Returns the date that this step was created.
     */
    public Date getStartDate();

    /**
     * Returns the status of this step.
     */
    public String getStatus();

    /**
     * Returns the ID of the step in the workflow definition.
     */
    public int getStepId();
}


text:
package scale

import (
	"context"
	"fmt"

	"github.com/go-graphite/carbonapi/expr/helper"
	"github.com/go-graphite/carbonapi/expr/interfaces"
	"github.com/go-graphite/carbonapi/expr/types"
	"github.com/go-graphite/carbonapi/pkg/parser"
)

type scale struct {
	interfaces.FunctionBase
}

func GetOrder() interfaces.Order {
	return interfaces.Any
}

func New(configFile string) []interfaces.FunctionMetadata {
	res := make([]interfaces.FunctionMetadata, 0)
	f := &scale{}
	functions := []string{"scale", "scaleAfterTimestamp"}
	for _, n := range functions {
		res = append(res, interfaces.FunctionMetadata{Name: n, F: f})
	}
	return res
}

// scale(seriesList, factor)
func (f *scale) Do(ctx context.Context, e parser.Expr, from, until int64, values map[parser.MetricRequest][]*types.MetricData) ([]*types.MetricData, error) {
	arg, err := helper.GetSeriesArg(e.Args()[0], from, until, values)
	if err != nil {
		return nil, err
	}
	scale, err := e.GetFloatArg(1)
	if err != nil {
		return nil, err
	}
	timestamp, err := e.GetIntArgDefault(2, 0)
	if err != nil {
		return nil, err
	}

	results := make([]*types.MetricData, 0, len(arg))
	for _, a := range arg {
		r := *a
		if timestamp == 0 {
			r.Name = fmt.Sprintf("scale(%s,%g)", a.Name, scale)
		} else {
			r.Name = fmt.Sprintf("scale(%s,%g,%d)", a.Name, scale, timestamp)
		}
		r.Values = make([]float64, len(a.Values))

		currentTimestamp := a.StartTime
		for i, v := range a.Values {
			r.Values[i] = v
			if currentTimestamp >= int64(timestamp) {
				r.Values[i] *= scale
			}

			currentTimestamp += a.StepTime
		}
		results = append(results, &r)
	}
	return results, nil
}

// Description is auto-generated description, based on output of https://github.com/graphite-project/graphite-web
func (f *scale) Description() map[string]types.FunctionDescription {
	return map[string]types.FunctionDescription{
		"scale": {
			Description: "Takes one metric or a wildcard seriesList followed by a constant, and multiplies the datapoint\n" +
				"by the constant provided at each point.\n"+
				"carbonapi extends this function by optional 3-rd parameter that accepts unix-timestamp. If provided, only values with timestamp newer than it will be scaled\n\n"+
				"Example:\n\n.. code-block:: none\n\n  &target=scale(Server.instance01.threads.busy,10)\n  &target=scale(Server.instance*.threads.busy,10)\n\n"+
				"Alias: scaleAfterTimestamp",
			Function:    "scale(seriesList, factor)",
			Group:       "Transform",
			Module:      "graphite.render.functions",
			Name:        "scale",
			Params: []types.FunctionParam{
				{
					Name:     "seriesList",
					Required: true,
					Type:     types.SeriesList,
				},
				{
					Name:     "factor",
					Required: true,
					Type:     types.Float,
				},
				{
					Name:     "timestamp",
					Required: false,
					Type:     types.Integer,
					Default:  types.NewSuggestion(0),
				},
			},
		},
		"scaleAfterTimestamp": {
			Description: "Takes one metric or a wildcard seriesList followed by a constant, and multiplies the datapoint\n" +
				"by the constant provided at each point.\n"+
				"carbonapi extends this function by optional 3-rd parameter that accepts unix-timestamp. If provided, only values with timestamp newer than it will be scaled\n\n"+
				"Example:\n\n.. code-block:: none\n\n  &target=scale(Server.instance01.threads.busy,10)\n  &target=scale(Server.instance*.threads.busy,10)",
			Function:    "scale(seriesList, factor)",
			Group:       "Transform",
			Module:      "graphite.render.functions",
			Name:        "scale",
			Params: []types.FunctionParam{
				{
					Name:     "seriesList",
					Required: true,
					Type:     types.SeriesList,
				},
				{
					Name:     "factor",
					Required: true,
					Type:     types.Float,
				},
				{
					Name:     "timestamp",
					Required: false,
					Type:     types.Integer,
					Default:  types.NewSuggestion(0),
				},
			},
		},
	}
}


text:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.primeton.storm</groupId>
	<artifactId>kafka</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>kafka</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<build>
		<plugins>
			<!-- <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<compilerVersion>1.6</compilerVersion>
				</configuration>
			</plugin> -->
			<!-- 第一种方式 mvn package assembly:single    -->
			<!-- <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.5.5</version>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.primeton.storm.topology.RateStaticsTopology</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<source>1.6</source>
					<target>1.6</target>
					<compilerVersion>1.6</compilerVersion>
				</configuration>
			</plugin> -->

			<!-- 第二种方式  mvn package -->
			<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.primeton.storm.topology.RateStaticsTopology</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
		</plugins>
	</build>

	<dependencies>

		<!-- Storm Dependency -->
		<dependency>
			<groupId>org.apache.storm</groupId>
			<artifactId>storm-core</artifactId>
			<version>1.1.0</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.storm/storm-kafka -->
		<dependency>
			<groupId>org.apache.storm</groupId>
			<artifactId>storm-kafka</artifactId>
			<version>1.1.0</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka_2.10 -->
		<dependency>
			<groupId>org.apache.kafka</groupId>
			<artifactId>kafka_2.10</artifactId>
			<version>0.10.2.1</version>

			<exclusions>
				<exclusion>
					<groupId>org.apache.zookeeper</groupId>
					<artifactId>zookeeper</artifactId>
				</exclusion>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.apache.kafka</groupId>
					<artifactId>kafka-clients</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>

			</exclusions>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
		<dependency>
			<groupId>org.apache.kafka</groupId>
			<artifactId>kafka-clients</artifactId>
			<version>0.10.2.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.31</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.5</version>
		</dependency>

	</dependencies>
</project>


text:
import NodeFS = require("fs");
import NodePath = require("path");
import async = require("async");

import "langext";

const SCRIPT_VERSION: string = "0.1.0-a1";

const ACTIONS_ROOT: string = "app/actions";
const SRT_PATH: string = "app/boot/static-router-table.php";
const DRT_PATH: string = "app/boot/dynamic-router-table.php";
const TAB: string = "    ";

let preTaskDate: number = new Date().getTime();
let buildTask: number[] = [];

interface HTTPMethodTable<T> extends Dictionary<T> {
    "ALL"?: T;
    "GET"?: T;
    "POST"?: T;
    "PUT"?: T;
    "PATCH"?: T;
    "DELETE"?: T;
    "HEAD"?: T;
    "OPTIONS"?: T;
    "CONNECT"?: T;
}

let filesEntityTable: string[] = [];
let actionFiles: string[] = [];
let routerFiles: string[] = [];

interface DynamicRouterRule {

    "exp": RegExp;

    "variables": string[];

    "path": string;
}

class RouterTable {

    "simple": Dictionary<string>;

    "dynamic": DynamicRouterRule[];

    "name": string;

    public constructor(name: string) {

        this.name = name;

        this.simple = {};

        this.dynamic = [];
    }

    public registerSimpleAction(filePath: string, uri: string) {

        console.info(`    - Registered: ${this.name} ${uri} -> ${filePath}`);

        this.simple[uri] = filePath;
    }

    public registerDynamicAction(filePath: string, uri: string, callback: ErrorCallback): void {

        let drr: DynamicRouterRule = RouterTable.compileDynamicRule(uri, filePath);

        if (!drr) {

            return callback({
                "name": "SYNTAX-ERROR",
                "message": `Unrecognizable URI expression "${uri}".`
            });
        }

        this.dynamic.push(drr);

        return callback();
    }

    protected static compileDynamicRule(rule: string, filePath: string): DynamicRouterRule {

        let drr: DynamicRouterRule = {
            "exp": null,
            "variables": [],
            "path": filePath
        };

        let varTypes: string[] = [];
        let i: number = 0;

        drr.exp = new RegExp("^" + RegExp.escape(rule.replace(/\{[#\*\$%]\w+\}/g, function(el: string): string {

            varTypes.push(el[1]);
            drr.variables.push(el.substr(2, el.length - 3));

            return "%%VB%%";

        })).replace(/%%VB%%/g, function(): string {

            switch (varTypes[i++]) {
            case "#": return "(\\d+)";
            case "%": return "(\\d+|\\d+\\.\\d+)";
            case "$": return "([^\\/]+)";
            case "*": return "(.+)";
            }

        }) + "$");

        return drr;
    }
}

/**
 * Configurations
 */

let optHTTPMethodStatus: HTTPMethodTable<boolean> = {};

let routerTables: HTTPMethodTable<RouterTable> = {};

let strictMode: boolean = false;

let ignoreInvalidFile: boolean = false;

let enabledDynamic: boolean = true;

let enabledAPCu: boolean = false;

let watchMode: boolean = false;

/**
 * These methods are enabled by default.
 */
for (let httpMethod of ["ALL", "GET", "POST", "PUT", "PATCH", "DELETE"]) {

    optHTTPMethodStatus[httpMethod] = true;
    routerTables[httpMethod] = new RouterTable(httpMethod);
}

/**
 * These methods are disabled by default.
 */
for (let httpMethod of ["HEAD", "OPTIONS", "CONNECT"]) {

    optHTTPMethodStatus[httpMethod] = false;
}

const HTTP_METHODS: string[] = ["ALL", "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "CONNECT"];

console.info(`Trame Static Router Table Build Tools v${SCRIPT_VERSION}\n`);

/**
 * Handle the arguments from console.
 */
for (let i: number = 2; i < process.argv.length; i++) {

    let argv: string = process.argv[i];

    switch (argv) {
    case "--strict-mode":

        console.info("    * Strict Mode is on.\n");

        strictMode = true;

        break;

    case "--watch":

        watchMode = true;

        break;

    case "--ignore-other-files":

        ignoreInvalidFile = true;

        break;

    case "--enable-apcu":

        console.info("    * APCu support is on.\n");

        enabledAPCu = true;

        break;

    case "--disable-dynamic":

        console.info("    * Dynamic router has been disabled.\n");

        enabledDynamic = false;

        break;

    case "--enable-all-http-methods":

        for (let httpMethod of HTTP_METHODS) {

            optHTTPMethodStatus[httpMethod] = true;
            routerTables[httpMethod] = new RouterTable(httpMethod);
        }

        console.info("    * All HTTP methods filter have been reset to be enabled.\n");

        break;

    case "--disable-all-http-methods":

        for (let httpMethod of HTTP_METHODS) {

            optHTTPMethodStatus[httpMethod] = false;
            delete routerTables[httpMethod];
        }

        console.info("    * All HTTP methods filter have been reset to be disabled.\n");

        break;

    default:

        if (argv.startsWith("--enable-http-")) {

            let httpMethod: string = argv.substr(14);

            if (HTTP_METHODS.indexOf(httpMethod) >= 0) {

                optHTTPMethodStatus[httpMethod] = true;
                routerTables[httpMethod] = new RouterTable(httpMethod);

                console.info(`    * HTTP methods filter "${httpMethod}" has been enabled.\n`);

            } else {

                console.warn(` - Invalid parameter "${argv}".`);
            }

        } else if (argv.startsWith("--disable-http-")) {

            let httpMethod: string = argv.substr(15);

            if (HTTP_METHODS.indexOf(httpMethod) >= 0) {

                optHTTPMethodStatus[httpMethod] = false;
                delete routerTables[httpMethod];

                console.info(`    * HTTP methods filter "${httpMethod}" has been disabled.\n`);

            } else {

                console.warn(`Invalid parameter "${argv}".`);
            }

        }
    }
}

/**
 * Concat two path into one.
 */
function concatPath(a: string, b: string) {

    if (NodePath.sep === "\\") {

        return NodePath.normalize(a + "/" + b).replace(/\\/g, "/");

    } else {

        return NodePath.normalize(a + "/" + b);
    }
}

/**
 * Concat two URI into one.
 */
function concatURI(a: string, b: string) {

    let started: boolean = b[0] === "/";
    let ended: boolean = a[a.length - 1] === "/";

    if (started && ended) {

        return `${a}${b.substr(1)}`;
    }

    if (started || ended) {

        return `${a}${b}`;
    }

    return `${a}/${b}`;
}

/**
 * Fix the URI if ends with "index" or is empty.
 */
function fixURI(uri: string): string {

    if (uri[uri.length - 1] === "/") {

        uri = uri.substr(0, uri.length - 1);
    }

    if (uri.endsWith("index")) {

        uri = uri.substr(0, uri.length - 6);
    }

    if (uri.length === 0) {

        uri = "/";
    }

    return uri;
}

/**
 * Add a file into simple table.
 */
function addDynamicRule(spMethods: string[], uri: string, file: string, callback: ErrorCallback): void {

    if (!uri.match(/\{[#\*\$%]\w+\}/)) {

        return addSimpleRule(spMethods, uri, file, callback);
    }

    let method = spMethods[0];

    if (optHTTPMethodStatus[method]) {

        return routerTables[method].registerDynamicAction(file, uri, function(err?: Error) {

            if (err) {

                if (strictMode) {

                    return callback(err);
                }

                console.warn(`[?] Warning: ${err.message}.`);

                return callback();
            }

            console.info(`    - Registered ${method} ${uri} -> ${file}`);

            return callback();
        });

    } else if (strictMode) {

        return callback({
            "name": "UNAVAIABLE-FILTER",
            "message": `HTTP methods filter "${method}" is disabled.`
        });

    } else {

        console.warn(`[?] Warning: HTTP methods filter "${method}" is disabled.`);

        return callback();
    }

}

/**
 * Add a file into simple table.
 */
function addSimpleRule(spMethods: string[], uri: string, file: string, callback: ErrorCallback): void {

    async.eachOfSeries<string>(spMethods, function(method: string, key: number, next: ErrorCallback): void {

        if (optHTTPMethodStatus[method]) {

            routerTables[method].registerSimpleAction(file, uri);

            return next();

        } else if (strictMode) {

            return next({
                "name": "UNAVAIABLE-FILTER",
                "message": `HTTP methods filter "${method}" is disabled.`
            });

        } else {

            console.warn(`[?] Warning: HTTP methods filter "${method}" is disabled.`);

            return next();
        }

    }, callback);
}

/**
 * Scan all files in actions directory.
 */
function scanRoutableFiles(dir: string, callback: ErrorCallback): void {

    NodeFS.readdir(dir, function(err: Error, items: string[]): void {

        if (err) {

            return callback(err);
        }

        async.eachOfSeries<string>(items, function(item: string, key: string, next: ErrorCallback): void {

            if (item === "." || item === "..") {

                return next();
            }

            let path: string = concatPath(dir, item);

            NodeFS.stat(path, function(err: Error, stats: NodeFS.Stats): void {

                if (err) {

                    return next(err);
                }

                if (stats.isDirectory()) {

                    if (item[0] !== ".") {

                        scanRoutableFiles(path, next);

                    } else {

                        next();
                    }

                } else {

                    if (item === ".router") {

                        routerFiles.push(path);

                    } else {

                        if (item[0] !== ".") {

                            actionFiles.push(path);

                        }

                        filesEntityTable.push(path);
                    }

                    next();
                }

            });

        }, callback);
    });

}

/**
 * Build the table of simple URI->File mappings.
 */
function buildSimpleTable(root: string, next: ErrorCallback): void {

    async.eachOfSeries<string>(actionFiles, function(file: string, key: number, next: ErrorCallback): void {

        let matches: RegExpMatchArray = file.match(/(.(GET|POST|PUT|PATCH|DELETE|HEAD|CONNECT|OPTIONS))*\.php$/);

        if (!matches) {

            if (ignoreInvalidFile) {

                console.warn(`[?] Warning: File "${file}" is not a php script file.`);

                return next();

            } else {

                return next({
                    "name": "INVALID-SCRIPT",
                    "message": `File "${file}" is not a php script file.`
                });

            }
        }

        return addSimpleRule(
            matches[1] ? matches[0].substr(1, matches[0].length - 5).split(".") : ["ALL"],
            fixURI(file.substr(root.length, file.length - root.length - matches[0].length)),
            file,
            next
        );
    }, next);
}

/**
 * Parse the ".router" files.
 */
function handleRouterFile(root: string, cwd: string, rules: string[], file: string, callback: ErrorCallback): void {

    async.eachOfSeries<string>(rules, function(rule: string, line: number, next: ErrorCallback): void {

        rule = rule.trim();

        if (rule.length === 0) {

            return next();
        }

        let matches: RegExpMatchArray = rule.match(/^([A-Z]+)\s+([\S]+)\s+([\S]+)$/);

        if (matches && HTTP_METHODS.indexOf(matches[1]) > -1) {

            let path: string = concatPath(cwd, matches[3]);

            NodeFS.exists(path, function(exist: boolean): void {

                if (!exist) {

                    return next({
                        "name": "NOT-FOUND",
                        "message": `File "${path}" doesn't exist.`
                    });
                }

                addDynamicRule(
                    [matches[1]],
                    fixURI(concatURI(NodePath.dirname(file).substr(root.length), matches[2])),
                    path,
                    next
                );
            });

            return;
        }

        if (strictMode) {

            return next({
                "name": "SYNTAX-ERROR",
                "message": `Syntax error in file "${file}" line ${line + 1}.`
            });

        } else {

            console.warn(`[?] Warning: Syntax error in file "${file}" line ${line + 1}.`);

            return next();
        }

    }, callback);
}

/**
 * Build the table of dynamic mappings.
 */
function buildDynamicTable(root: string, callback: ErrorCallback): void {

    async.eachOfSeries<string>(routerFiles, function(file: string, key: number, next: ErrorCallback): void {

        NodeFS.readFile(file, "utf-8", function(err: Error, data: string): void {

            data.replace(/\r\n/g, "\n").replace(/\r/g, "\n");

            if (err) {

                return next({
                    "name": "BAD-ROUTER-FILE",
                    "message": `Failed to read the file "${file}".`
                });
            }

            handleRouterFile(
                root,
                NodePath.dirname(file),
                data.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n"),
                file,
                next
            );

        });

    }, callback);
}

function generateStaticTable(root: string, tablePath: string, callback: ErrorCallback): void {

    let tables: string[] = [];

    for (let method in routerTables) {

        let table: RouterTable = routerTables[method];

        let maps: string[] = [];

        for (let uri in table.simple) {

            let path: string = table.simple[uri];

            maps.push(`${TAB.repeat(2)}'${uri}' => '${path}'`);

        }

        if (maps.length > 0) {
            tables.push(
`${TAB}'${method}' => [
${maps.join(",\n")}
${TAB}]`
            );
        }
    }

    let srt: string = `<?php
return [
${tables.join(",\n")}
];
`;

    NodeFS.writeFile(tablePath, srt, callback);

}

function generateDynamicTable(root: string, tablePath: string, callback: ErrorCallback): void {

    let tables: string[] = [];

    for (let method in routerTables) {

        let table: RouterTable = routerTables[method];

        let maps: string[] = [];

        for (let dr of table.dynamic) {

            let exp: string = JSON.stringify(dr.exp.source);

            exp = `/${exp.substr(1, exp.length - 2)}/`;

            maps.push(
`${TAB.repeat(2)}[
${TAB.repeat(3)}'expr' => '${exp}',
${TAB.repeat(3)}'path' => '${dr.path}',
${TAB.repeat(3)}'vars' => ${JSON.stringify(dr.variables)}
${TAB.repeat(2)}]`
            );

        }

        if (maps.length > 0) {
            tables.push(
`${TAB}'${method}' => [
${maps.join(",\n")}
${TAB}]`
            );
        }
    }

    let drt: string = `<?php
return [
${tables.join(",\n")}
];
`;

    NodeFS.writeFile(tablePath, drt, callback);

}

function startBuilder(ts: number) {

    if (ts !== preTaskDate) {

        return;
    }

    buildTask.push(ts);

    if (buildTask.length > 1) {

        return;
    }

    async.series([

        function (next: ErrorCallback): void {

            filesEntityTable  = [];
            actionFiles = [];
            routerFiles = [];

            routerTables = {};

            for (let method of HTTP_METHODS) {

                if (optHTTPMethodStatus[method]) {

                    routerTables[method] = new RouterTable(method);
                }
            }

            next();
        },

        function (next: ErrorCallback): void {

            scanRoutableFiles(ACTIONS_ROOT, next);
        },

        function (next: ErrorCallback): void {

            console.info(
`File scan completed, found:\n
    ${filesEntityTable.length} Actions files
    ${actionFiles.length} Simple Actions files
    ${routerFiles.length} Advanced Router files\n
Building Simple Router Table:\n`
            );

            buildSimpleTable(ACTIONS_ROOT, next);
        },

        function (next: ErrorCallback): void {

            if (enabledDynamic) {

                console.info("\nBuilding Dynamic Router Table:\n");

                buildDynamicTable(ACTIONS_ROOT, next);

            } else {

                next();
            }
        },

        function (next: ErrorCallback): void {

            generateStaticTable(ACTIONS_ROOT, SRT_PATH, next);
        },

        function (next: ErrorCallback): void {

            generateDynamicTable(ACTIONS_ROOT, DRT_PATH, next);
        }

    ], function(err?: Error) {

        console.log("");

        if (err) {

            console.error(`[!] Error [${err.name}]: ${err.message}.`);

        } else {

            console.info("Router table has been successfully built.");
        }

        buildTask.shift();

        if (buildTask.length > 0) {

            preTaskDate = buildTask[0];
            startBuilder(preTaskDate);
        }

    });

}

watchMode && NodeFS.watch(ACTIONS_ROOT, function(event: string, file: string): void {

    if (event === "change") {

        return;
    }

    console.info(`\nFile changes detected, a new built task started.\n`);

    preTaskDate = new Date().getTime();

    setTimeout(startBuilder, 500, preTaskDate);
});

startBuilder(preTaskDate);


text:
package protocol

import (
	"crypto/rand"
	"fmt"
	"reflect"
)

// RandReader is the random reader the protocol package will use to read
// random bytes from. This is exported for testing, and should not be used.
var RandReader = rand.Reader

const idempotencyTokenFillTag = `idempotencyToken`

// CanSetIdempotencyToken returns true if the struct field should be
// automatically populated with a Idempotency token.
//
// Only *string and string type fields that are tagged with idempotencyToken
// which are not already set can be auto filled.
func CanSetIdempotencyToken(v reflect.Value, f reflect.StructField) bool {
	switch u := v.Interface().(type) {
	// To auto fill an Idempotency token the field must be a string,
	// tagged for auto fill, and have a zero value.
	case *string:
		return u == nil && len(f.Tag.Get(idempotencyTokenFillTag)) != 0
	case string:
		return len(u) == 0 && len(f.Tag.Get(idempotencyTokenFillTag)) != 0
	}

	return false
}

// GetIdempotencyToken returns a randomly generated idempotency token.
func GetIdempotencyToken() string {
	b := make([]byte, 16)
	RandReader.Read(b)

	return UUIDVersion4(b)
}

// SetIdempotencyToken will set the value provided with a Idempotency Token.
// Given that the value can be set. Will panic if value is not setable.
func SetIdempotencyToken(v reflect.Value) {
	if v.Kind() == reflect.Ptr {
		if v.IsNil() && v.CanSet() {
			v.Set(reflect.New(v.Type().Elem()))
		}
		v = v.Elem()
	}
	v = reflect.Indirect(v)

	if !v.CanSet() {
		panic(fmt.Sprintf("unable to set idempotnecy token %v", v))
	}

	b := make([]byte, 16)
	_, err := rand.Read(b)
	if err != nil {
		// TODO handle error
		return
	}

	v.Set(reflect.ValueOf(UUIDVersion4(b)))
}

// UUIDVersion4 returns a Version 4 random UUID from the byte slice provided
func UUIDVersion4(u []byte) string {
	// https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
	// 13th character is "4"
	u[6] = (u[6] | 0x40) & 0x4F
	// 17th character is "8", "9", "a", or "b"
	u[8] = (u[8] | 0x80) & 0xBF

	return fmt.Sprintf(`%X-%X-%X-%X-%X`, u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
}
//Added a line for testing
//Adding another line for Git event testing part 2
//Adding another line for Git event testing part 2.1
//Adding another line for Git event testing part 2.2


text:
schock
======

"We need [new technology], not these unmaintainable bash scripts."

- Anonymous

Introduction
------------

schock is a web services framework written (mostly) in bash.  It was built to provide rapid
development, experimentation, and testing with minimal effort.  Depending on the code you
plug into it, it should be reasonably secure, and fast enough for development work.

Use this in production at your own peril.  **It's the result of a bunch of hacking over a couple of days, and probably has real problems.  The interfaces definitely aren't stable.**

The main features include:

* command-line testing interface
* CGI support (e.g. nginx + fcgiwrap = working service)
* Processing of query strings, and form bodies.
* A JSON structured response builder.
* URI binding and routing.
* Being able to script POSIX like a normal human, rather than calling ```system``` over and over again.

Example
-------

A basic web service:

```shell
#!/bin/bash

SCHOCKDIR=$(dirname $0)/..
. $SCHOCKDIR/kitchensink.bash

hw () {
    success 200 "Hello, World!"
}

s::route::bind::uri "/hw" "GET" hw
s::run "$@"
```

Testing a URI endpoint at the command-line:

    schock$ examples/helloworld GET /hw
    INFO: binding in route '^/hw$' 'GET' 'hw'
    DEBUG: Dumping passed in arguments:
    DEBUG: looking up handler for GET on '/hw'
    DEBUG: handler is 'hw /hw'
    DEBUG: executing handler with args '/hw'
    {
        "message": "Hello, World!"
    }
    INFO: bailing...

Questions & Answers
-------------------

* Isn't this a dumb idea?

    No James, it lets you prototype system services quickly.

* But what about that one time I was using bash and had a quoting/globbing/whitespace/$OTHER problem?

    By default, schock turns on a more strict environment (e.g. noglob, pipefail, IFS=$'\n').  This
	Helps you help yourself.  Don't use globs for interacting with files.  Don't expect word splitting
	on space.  It can be annoying to learn to write shell scripts with those constraints, but it also
	closes the doors on some dumb mistakes.

* Will this work with ksh/pdksh/zsh/csh/tcsh/fish/...?

	Maybe, but probably not.  This code uses some bash-specifics.

* What license is this code under?

	All code in this repository is under the license defined in ```LICENSE```.  It's just a BSD 3-clause.


text:
package secret

import (
	"fmt"
	"time"

	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	pkgruntime "k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/watch"
	"k8s.io/client-go/tools/cache"
	"k8s.io/client-go/util/flowcontrol"
	federationapi "k8s.io/kubernetes/federation/apis/federation/v1beta1"
	federationclientset "k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset"
	"k8s.io/kubernetes/federation/pkg/federation-controller/util"
	"k8s.io/kubernetes/federation/pkg/federation-controller/util/deletionhelper"
	"k8s.io/kubernetes/federation/pkg/federation-controller/util/eventsink"
	"k8s.io/kubernetes/pkg/api"
	apiv1 "k8s.io/kubernetes/pkg/api/v1"
	kubeclientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
	"k8s.io/kubernetes/pkg/client/record"
	"k8s.io/kubernetes/pkg/controller"

	"github.com/golang/glog"
)

const (
	allClustersKey = "ALL_CLUSTERS"
)

type SecretController struct {
	// For triggering single secret reconciliation. This is used when there is an
	// add/update/delete operation on a secret in either federated API server or
	// in some member of the federation.
	secretDeliverer *util.DelayingDeliverer

	// For triggering all secrets reconciliation. This is used when
	// a new cluster becomes available.
	clusterDeliverer *util.DelayingDeliverer

	// Contains secrets present in members of federation.
	secretFederatedInformer util.FederatedInformer
	// For updating members of federation.
	federatedUpdater util.FederatedUpdater
	// Definitions of secrets that should be federated.
	secretInformerStore cache.Store
	// Informer controller for secrets that should be federated.
	secretInformerController cache.Controller

	// Client to federated api server.
	federatedApiClient federationclientset.Interface

	// Backoff manager for secrets
	secretBackoff *flowcontrol.Backoff

	// For events
	eventRecorder record.EventRecorder

	deletionHelper *deletionhelper.DeletionHelper

	secretReviewDelay     time.Duration
	clusterAvailableDelay time.Duration
	smallDelay            time.Duration
	updateTimeout         time.Duration
}

// NewSecretController returns a new secret controller
func NewSecretController(client federationclientset.Interface) *SecretController {
	broadcaster := record.NewBroadcaster()
	broadcaster.StartRecordingToSink(eventsink.NewFederatedEventSink(client))
	recorder := broadcaster.NewRecorder(apiv1.EventSource{Component: "federated-secrets-controller"})

	secretcontroller := &SecretController{
		federatedApiClient:    client,
		secretReviewDelay:     time.Second * 10,
		clusterAvailableDelay: time.Second * 20,
		smallDelay:            time.Second * 3,
		updateTimeout:         time.Second * 30,
		secretBackoff:         flowcontrol.NewBackOff(5*time.Second, time.Minute),
		eventRecorder:         recorder,
	}

	// Build delivereres for triggering reconciliations.
	secretcontroller.secretDeliverer = util.NewDelayingDeliverer()
	secretcontroller.clusterDeliverer = util.NewDelayingDeliverer()

	// Start informer in federated API servers on secrets that should be federated.
	secretcontroller.secretInformerStore, secretcontroller.secretInformerController = cache.NewInformer(
		&cache.ListWatch{
			ListFunc: func(options metav1.ListOptions) (pkgruntime.Object, error) {
				return client.Core().Secrets(metav1.NamespaceAll).List(options)
			},
			WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
				return client.Core().Secrets(metav1.NamespaceAll).Watch(options)
			},
		},
		&apiv1.Secret{},
		controller.NoResyncPeriodFunc(),
		util.NewTriggerOnAllChanges(func(obj pkgruntime.Object) { secretcontroller.deliverSecretObj(obj, 0, false) }))

	// Federated informer on secrets in members of federation.
	secretcontroller.secretFederatedInformer = util.NewFederatedInformer(
		client,
		func(cluster *federationapi.Cluster, targetClient kubeclientset.Interface) (cache.Store, cache.Controller) {
			return cache.NewInformer(
				&cache.ListWatch{
					ListFunc: func(options metav1.ListOptions) (pkgruntime.Object, error) {
						return targetClient.Core().Secrets(metav1.NamespaceAll).List(options)
					},
					WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
						return targetClient.Core().Secrets(metav1.NamespaceAll).Watch(options)
					},
				},
				&apiv1.Secret{},
				controller.NoResyncPeriodFunc(),
				// Trigger reconciliation whenever something in federated cluster is changed. In most cases it
				// would be just confirmation that some secret operation succeeded.
				util.NewTriggerOnAllChanges(
					func(obj pkgruntime.Object) {
						secretcontroller.deliverSecretObj(obj, secretcontroller.secretReviewDelay, false)
					},
				))
		},

		&util.ClusterLifecycleHandlerFuncs{
			ClusterAvailable: func(cluster *federationapi.Cluster) {
				// When new cluster becomes available process all the secrets again.
				secretcontroller.clusterDeliverer.DeliverAt(allClustersKey, nil, time.Now().Add(secretcontroller.clusterAvailableDelay))
			},
		},
	)

	// Federated updeater along with Create/Update/Delete operations.
	secretcontroller.federatedUpdater = util.NewFederatedUpdater(secretcontroller.secretFederatedInformer,
		func(client kubeclientset.Interface, obj pkgruntime.Object) error {
			secret := obj.(*apiv1.Secret)
			_, err := client.Core().Secrets(secret.Namespace).Create(secret)
			return err
		},
		func(client kubeclientset.Interface, obj pkgruntime.Object) error {
			secret := obj.(*apiv1.Secret)
			_, err := client.Core().Secrets(secret.Namespace).Update(secret)
			return err
		},
		func(client kubeclientset.Interface, obj pkgruntime.Object) error {
			secret := obj.(*apiv1.Secret)
			err := client.Core().Secrets(secret.Namespace).Delete(secret.Name, &metav1.DeleteOptions{})
			return err
		})

	secretcontroller.deletionHelper = deletionhelper.NewDeletionHelper(
		secretcontroller.hasFinalizerFunc,
		secretcontroller.removeFinalizerFunc,
		secretcontroller.addFinalizerFunc,
		// objNameFunc
		func(obj pkgruntime.Object) string {
			secret := obj.(*apiv1.Secret)
			return secret.Name
		},
		secretcontroller.updateTimeout,
		secretcontroller.eventRecorder,
		secretcontroller.secretFederatedInformer,
		secretcontroller.federatedUpdater,
	)

	return secretcontroller
}

// Returns true if the given object has the given finalizer in its ObjectMeta.
func (secretcontroller *SecretController) hasFinalizerFunc(obj pkgruntime.Object, finalizer string) bool {
	secret := obj.(*apiv1.Secret)
	for i := range secret.ObjectMeta.Finalizers {
		if string(secret.ObjectMeta.Finalizers[i]) == finalizer {
			return true
		}
	}
	return false
}

// Removes the finalizer from the given objects ObjectMeta.
// Assumes that the given object is a secret.
func (secretcontroller *SecretController) removeFinalizerFunc(obj pkgruntime.Object, finalizer string) (pkgruntime.Object, error) {
	secret := obj.(*apiv1.Secret)
	newFinalizers := []string{}
	hasFinalizer := false
	for i := range secret.ObjectMeta.Finalizers {
		if string(secret.ObjectMeta.Finalizers[i]) != finalizer {
			newFinalizers = append(newFinalizers, secret.ObjectMeta.Finalizers[i])
		} else {
			hasFinalizer = true
		}
	}
	if !hasFinalizer {
		// Nothing to do.
		return obj, nil
	}
	secret.ObjectMeta.Finalizers = newFinalizers
	secret, err := secretcontroller.federatedApiClient.Core().Secrets(secret.Namespace).Update(secret)
	if err != nil {
		return nil, fmt.Errorf("failed to remove finalizer %s from secret %s: %v", finalizer, secret.Name, err)
	}
	return secret, nil
}

// Adds the given finalizer to the given objects ObjectMeta.
// Assumes that the given object is a secret.
func (secretcontroller *SecretController) addFinalizerFunc(obj pkgruntime.Object, finalizer string) (pkgruntime.Object, error) {
	secret := obj.(*apiv1.Secret)
	secret.ObjectMeta.Finalizers = append(secret.ObjectMeta.Finalizers, finalizer)
	secret, err := secretcontroller.federatedApiClient.Core().Secrets(secret.Namespace).Update(secret)
	if err != nil {
		return nil, fmt.Errorf("failed to add finalizer %s to secret %s: %v", finalizer, secret.Name, err)
	}
	return secret, nil
}

func (secretcontroller *SecretController) Run(stopChan <-chan struct{}) {
	go secretcontroller.secretInformerController.Run(stopChan)
	secretcontroller.secretFederatedInformer.Start()
	go func() {
		<-stopChan
		secretcontroller.secretFederatedInformer.Stop()
	}()
	secretcontroller.secretDeliverer.StartWithHandler(func(item *util.DelayingDelivererItem) {
		secret := item.Value.(*types.NamespacedName)
		secretcontroller.reconcileSecret(*secret)
	})
	secretcontroller.clusterDeliverer.StartWithHandler(func(_ *util.DelayingDelivererItem) {
		secretcontroller.reconcileSecretsOnClusterChange()
	})
	util.StartBackoffGC(secretcontroller.secretBackoff, stopChan)
}

func (secretcontroller *SecretController) deliverSecretObj(obj interface{}, delay time.Duration, failed bool) {
	secret := obj.(*apiv1.Secret)
	secretcontroller.deliverSecret(types.NamespacedName{Namespace: secret.Namespace, Name: secret.Name}, delay, failed)
}

// Adds backoff to delay if this delivery is related to some failure. Resets backoff if there was no failure.
func (secretcontroller *SecretController) deliverSecret(secret types.NamespacedName, delay time.Duration, failed bool) {
	key := secret.String()
	if failed {
		secretcontroller.secretBackoff.Next(key, time.Now())
		delay = delay + secretcontroller.secretBackoff.Get(key)
	} else {
		secretcontroller.secretBackoff.Reset(key)
	}
	secretcontroller.secretDeliverer.DeliverAfter(key, &secret, delay)
}

// Check whether all data stores are in sync. False is returned if any of the informer/stores is not yet
// synced with the corresponding api server.
func (secretcontroller *SecretController) isSynced() bool {
	if !secretcontroller.secretFederatedInformer.ClustersSynced() {
		glog.V(2).Infof("Cluster list not synced")
		return false
	}
	clusters, err := secretcontroller.secretFederatedInformer.GetReadyClusters()
	if err != nil {
		glog.Errorf("Failed to get ready clusters: %v", err)
		return false
	}
	if !secretcontroller.secretFederatedInformer.GetTargetStore().ClustersSynced(clusters) {
		return false
	}
	return true
}

// The function triggers reconciliation of all federated secrets.
func (secretcontroller *SecretController) reconcileSecretsOnClusterChange() {
	if !secretcontroller.isSynced() {
		secretcontroller.clusterDeliverer.DeliverAt(allClustersKey, nil, time.Now().Add(secretcontroller.clusterAvailableDelay))
	}
	for _, obj := range secretcontroller.secretInformerStore.List() {
		secret := obj.(*apiv1.Secret)
		secretcontroller.deliverSecret(types.NamespacedName{Namespace: secret.Namespace, Name: secret.Name}, secretcontroller.smallDelay, false)
	}
}

func (secretcontroller *SecretController) reconcileSecret(secret types.NamespacedName) {
	if !secretcontroller.isSynced() {
		secretcontroller.deliverSecret(secret, secretcontroller.clusterAvailableDelay, false)
		return
	}

	key := secret.String()
	baseSecretObjFromStore, exist, err := secretcontroller.secretInformerStore.GetByKey(key)
	if err != nil {
		glog.Errorf("Failed to query main secret store for %v: %v", key, err)
		secretcontroller.deliverSecret(secret, 0, true)
		return
	}

	if !exist {
		// Not federated secret, ignoring.
		return
	}

	// Create a copy before modifying the obj to prevent race condition with
	// other readers of obj from store.
	baseSecretObj, err := api.Scheme.DeepCopy(baseSecretObjFromStore)
	baseSecret, ok := baseSecretObj.(*apiv1.Secret)
	if err != nil || !ok {
		glog.Errorf("Error in retrieving obj from store: %v, %v", ok, err)
		secretcontroller.deliverSecret(secret, 0, true)
		return
	}
	if baseSecret.DeletionTimestamp != nil {
		if err := secretcontroller.delete(baseSecret); err != nil {
			glog.Errorf("Failed to delete %s: %v", secret, err)
			secretcontroller.eventRecorder.Eventf(baseSecret, api.EventTypeNormal, "DeleteFailed",
				"Secret delete failed: %v", err)
			secretcontroller.deliverSecret(secret, 0, true)
		}
		return
	}

	glog.V(3).Infof("Ensuring delete object from underlying clusters finalizer for secret: %s",
		baseSecret.Name)
	// Add the required finalizers before creating a secret in underlying clusters.
	updatedSecretObj, err := secretcontroller.deletionHelper.EnsureFinalizers(baseSecret)
	if err != nil {
		glog.Errorf("Failed to ensure delete object from underlying clusters finalizer in secret %s: %v",
			baseSecret.Name, err)
		secretcontroller.deliverSecret(secret, 0, false)
		return
	}
	baseSecret = updatedSecretObj.(*apiv1.Secret)

	glog.V(3).Infof("Syncing secret %s in underlying clusters", baseSecret.Name)

	clusters, err := secretcontroller.secretFederatedInformer.GetReadyClusters()
	if err != nil {
		glog.Errorf("Failed to get cluster list: %v", err)
		secretcontroller.deliverSecret(secret, secretcontroller.clusterAvailableDelay, false)
		return
	}

	operations := make([]util.FederatedOperation, 0)
	for _, cluster := range clusters {
		clusterSecretObj, found, err := secretcontroller.secretFederatedInformer.GetTargetStore().GetByKey(cluster.Name, key)
		if err != nil {
			glog.Errorf("Failed to get %s from %s: %v", key, cluster.Name, err)
			secretcontroller.deliverSecret(secret, 0, true)
			return
		}

		// The data should not be modified.
		desiredSecret := &apiv1.Secret{
			ObjectMeta: util.DeepCopyRelevantObjectMeta(baseSecret.ObjectMeta),
			Data:       baseSecret.Data,
			Type:       baseSecret.Type,
		}

		if !found {
			secretcontroller.eventRecorder.Eventf(baseSecret, api.EventTypeNormal, "CreateInCluster",
				"Creating secret in cluster %s", cluster.Name)

			operations = append(operations, util.FederatedOperation{
				Type:        util.OperationTypeAdd,
				Obj:         desiredSecret,
				ClusterName: cluster.Name,
			})
		} else {
			clusterSecret := clusterSecretObj.(*apiv1.Secret)

			// Update existing secret, if needed.
			if !util.SecretEquivalent(*desiredSecret, *clusterSecret) {

				secretcontroller.eventRecorder.Eventf(baseSecret, api.EventTypeNormal, "UpdateInCluster",
					"Updating secret in cluster %s", cluster.Name)
				operations = append(operations, util.FederatedOperation{
					Type:        util.OperationTypeUpdate,
					Obj:         desiredSecret,
					ClusterName: cluster.Name,
				})
			}
		}
	}

	if len(operations) == 0 {
		// Everything is in order
		return
	}
	err = secretcontroller.federatedUpdater.UpdateWithOnError(operations, secretcontroller.updateTimeout,
		func(op util.FederatedOperation, operror error) {
			secretcontroller.eventRecorder.Eventf(baseSecret, api.EventTypeNormal, "UpdateInClusterFailed",
				"Secret update in cluster %s failed: %v", op.ClusterName, operror)
		})

	if err != nil {
		glog.Errorf("Failed to execute updates for %s: %v", key, err)
		secretcontroller.deliverSecret(secret, 0, true)
		return
	}

	// Evertyhing is in order but lets be double sure
	secretcontroller.deliverSecret(secret, secretcontroller.secretReviewDelay, false)
}

// delete deletes the given secret or returns error if the deletion was not complete.
func (secretcontroller *SecretController) delete(secret *apiv1.Secret) error {
	glog.V(3).Infof("Handling deletion of secret: %v", *secret)
	_, err := secretcontroller.deletionHelper.HandleObjectInUnderlyingClusters(secret)
	if err != nil {
		return err
	}

	err = secretcontroller.federatedApiClient.Core().Secrets(secret.Namespace).Delete(secret.Name, nil)
	if err != nil {
		// Its all good if the error is not found error. That means it is deleted already and we do not have to do anything.
		// This is expected when we are processing an update as a result of secret finalizer deletion.
		// The process that deleted the last finalizer is also going to delete the secret and we do not have to do anything.
		if !errors.IsNotFound(err) {
			return fmt.Errorf("failed to delete secret: %v", err)
		}
	}
	return nil
}


text:
<!DOCTYPE html>

  <title>RSpec results</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Expires" content="-1" />
  <meta http-equiv="Pragma" content="no-cache" />

RSpec Code Examples

    <input id="passed_checkbox"  name="passed_checkbox"  type="checkbox" checked="checked" onchange="apply_filters()" value="1" /> Passed
    <input id="failed_checkbox"  name="failed_checkbox"  type="checkbox" checked="checked" onchange="apply_filters()" value="2" /> Failed
    <input id="pending_checkbox" name="pending_checkbox" type="checkbox" checked="checked" onchange="apply_filters()" value="3" /> Pending

pending spec with no implementation

is pending (PENDING: Not yet implemented)

pending command with block format

with content that would fail

is pending (PENDING: No reason given)

behaves like shared

      is marked as pending but passes
      n.nnnns

Expected pending 'No reason given' to fail. No Error was raised.
Shared Example Group: "shared" called from ./spec/rspec/core/resources/formatter_specs.rb:22

./spec/rspec/core/resources/formatter_specs.rb:4

2
3RSpec.shared_examples_for "shared" do
4  it "is marked as pending but passes" do
5    pending
6    expect(1).to eq(1)

passing spec

passesn.nnnns

failing spec

      fails
      n.nnnns

Failure/Error: expect(1).to eq(2)

  expected: 2
       got: 1

  (compared using ==)

./spec/rspec/core/resources/formatter_specs.rb:33:in `block (2 levels) in <top (required)>'

31RSpec.describe "failing spec" do
32  it "fails" do
33    expect(1).to eq(2)
34  end
35end

a failing spec with odd backtraces

      fails with a backtrace that has no file
      n.nnnns

Failure/Error: ERB.new("<%= raise 'foo' %>").result

RuntimeError:
  foo

./spec/rspec/core/resources/formatter_specs.rb:41:in `block (2 levels) in <top (required)>'

39    require 'erb'
40
41    ERB.new("<%= raise 'foo' %>").result
42  end

      fails with a backtrace containing an erb file
      n.nnnns

Failure/Error: Unable to find /foo.html.erb to read failed line

Exception:
  Exception

/foo.html.erb:1:in `<main>': foo (RuntimeError)
   from /lib/ruby/1.9.1/erb.rb:753:in `eval'

  Showing full backtrace because every line was filtered out.
  See docs for RSpec::Configuration#backtrace_exclusion_patterns and
  RSpec::Configuration#backtrace_inclusion_patterns for more information.

-1# Couldn't get snippet for

with a `nil` backtrace

      raises
      n.nnnns

Failure/Error: Unable to find matching line from backtrace

RuntimeError:
  boom

-1# Couldn't get snippet for