小编Mar*_*ing的帖子

如何测试Python 3.4 asyncio代码?

使用Python 3.4 asyncio库编写代码单元测试的最佳方法是什么?假设我想测试TCP客户端(SocketConnection):

import asyncio
import unittest

class TestSocketConnection(unittest.TestCase):
    def setUp(self):
        self.mock_server = MockServer("localhost", 1337)
        self.socket_connection = SocketConnection("localhost", 1337)

    @asyncio.coroutine
    def test_sends_handshake_after_connect(self):
        yield from self.socket_connection.connect()
        self.assertTrue(self.mock_server.received_handshake())
Run Code Online (Sandbox Code Playgroud)

当使用默认测试运行器运行此测试用例时,测试将始终成功,因为该方法仅执行到第一yield from条指令,之后它在执行任何断言之前返回.这导致测试总是成功.

是否有预先构建的测试运行器能够处理这样的异步代码?

python unit-testing python-3.x python-unittest python-asyncio

66
推荐指数
7
解决办法
2万
查看次数

git - 设置一个没有rebase的commit的父级

我曾经git-svn创建过SVN存储库的git镜像.SVN内部的结构有点不合标准,因此git创建了一个与分支没有共同提交的master分支.

      A---B---C topic

D---E---F---G master
Run Code Online (Sandbox Code Playgroud)

我知道提交A是基于提交的E,我非常肯定我已经解决了导致git无法识别该事实(使用filter-branch)的问题.我想要做的是重新连接topicmaster分支,设置E为以下的父代A:

      A---B---C topic
     /
D---E---F---G master
Run Code Online (Sandbox Code Playgroud)

git-rebase似乎对我没有用,因为提交的差异A列出了已经存在的大量文件的创建master,导致了大量的冲突.
从我对git的理解只是设置E为父母A应该足以解决所有问题.
这可能吗?如果是,我该怎么办?

git rebase

23
推荐指数
3
解决办法
6006
查看次数

Android Opengl ES:GLUtils.glTexImage2D导致GL_INVALID_ENUM

我正在尝试使用Android 2.2渲染一个简单的纹理四边形GLSurfaceView.我正在加载一个BMP图像(128x128)BitmapFactory.decodeResource()- 这似乎工作.但每当我尝试将这个位图放入OpenGL纹理时,GLUtils.glTexImage2D我会得到一个OpenGL错误:glGetError()返回1280 , GL_INVALID_ENUM. 我究竟做错了什么?这是我的渲染器的代码:

public class MyRenderer implements GLSurfaceView.Renderer {
    Context context;
    int texId;

    public MyRenderer(Context c) {
        this.context = c;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glEnable(GL10.GL_TEXTURE_2D);
        this.texId = loadTexture(gl);       
    }

    int loadTexture(GL10 gl) {      
        int[] tmp = new int[1];
        gl.glGenTextures(1, tmp, 0);
        int id = tmp[0];

        Bitmap bmp = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.myimage);
        gl.glGetError();
        GLUtils.texImage2D(id, 0, bmp, 0);
        int err = gl.glGetError();
        if (err != 0) …
Run Code Online (Sandbox Code Playgroud)

android opengl-es

8
推荐指数
1
解决办法
7837
查看次数

iOS上的MP3流媒体

我想用OpenAL在iOS游戏中播放音乐.音乐文件以mp3格式存储,我想使用缓冲队列来传输它们.我使用AudioFileReadPacketData()将音频数据加载到缓冲区中.但是播放缓冲区只会给我带来噪音.它适用于caf文件,但不适用于mp3.我是否错过了解码文件的一些重要步骤?

代码我用来打开声音文件:

- (void) openFile:(NSString*)fileName {
   NSBundle *bundle = [NSBundle mainBundle];
   CFURLRef url = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:fileName ofType:@"mp3"]] retain];
   AudioFileOpenURL(url, kAudioFileReadPermission, 0, &audioFile);
   AudioStreamBasicDescription theFormat;
   UInt32 formatSize = sizeof(theFormat);
   AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &formatSize, &theFormat);  
   freq = (ALsizei)theFormat.mSampleRate;
   CFRelease(url);
}
Run Code Online (Sandbox Code Playgroud)

我用来填充缓冲区的代码:

- (void) loadOneChunkIntoBuffer:(ALuint)buffer {
    char data[STREAM_BUFFER_SIZE];
    UInt32 loadSize = STREAM_BUFFER_SIZE;
    AudioStreamPacketDescription packetDesc[STREAM_PACKETS];
    UInt32 numPackets = STREAM_PACKETS;
    AudioFileReadPacketData(audioFile, NO, &loadSize, packetDesc, packetsLoaded, &numPackets, data);
    alBufferData(buffer, AL_FORMAT_STEREO16, data, loadSize, freq);
    packetsLoaded += numPackets;
}
Run Code Online (Sandbox Code Playgroud)

iphone openal

7
推荐指数
1
解决办法
5869
查看次数

Firebase安全规则:允许读取除一个字段之外的任何内容

在我的Firebase安全规则中,我希望匿名用户能够读取除一个字段(secret_field)之外的任何内容:

{
  "rules": {
    ".read": true,
    ".write": "auth != null",
    "stuff" : {
      "$stuffID" : {
        "secret_field" : {
           ".read" : "auth != null"
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,在Firebase中,如果secret_field评估为true 的方式上有任何读取规则,secret field则授予读取访问权限.

有没有办法扭转这种行为?(如果有任何读取规则secret_field评估为false,则禁止读取访问)

firebase

5
推荐指数
1
解决办法
1603
查看次数

惯用STL:迭代列表并插入元素

我正在编写一个迭代点列表的算法,计算它们之间的距离,如果距离太大,则插入额外的点.然而,我似乎缺乏对STL的熟悉,无法提出一个优雅的解决方案.我希望我能学到一些东西,所以我只会告诉你我的代码.你可能会给我一些提示.

for (std::list<PathPoint>::iterator it = ++points_.begin();
     it != points_.end(); it++)
{
    Vector curPos = it->getPosition();
    Vector prevPos = (--it)->getPosition();
    Vector vecFromPrev = curPos - prevPos;
    float distance = vecFromPrev.abs();
    it++;
    if (distance > MAX_DISTANCE_BETWEEN_POINTS)
    {               
        int pointsToInsert = (int)(distance / MAX_DISTANCE_BETWEEN_POINTS);             
        Vector curPos = prevPos;                
        for (int i = 0; i < pointsToInsert; i++)
        {
            curPos += vecFromPrev / pointsToInsert;
            it = points_.insert(it, PathPoint(curPos, false));
            it++;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl

1
推荐指数
1
解决办法
1719
查看次数