@@ -296,6 +296,16 @@ describe('openbci-sdk',function() {
296296 ( ourBoard . impedanceTest . onChannel ) . should . equal ( 0 ) ;
297297 ( ourBoard . impedanceTest . sampleNumber ) . should . equal ( 0 ) ;
298298 } ) ;
299+ it ( 'configures sync object correctly' , function ( ) {
300+ ourBoard = new openBCIBoard . OpenBCIBoard ( ) ;
301+ expect ( ourBoard . sync . curSyncObj ) . to . be . null ;
302+ expect ( ourBoard . sync . eventEmitter ) . to . be . null ;
303+ expect ( ourBoard . sync . objArray . length ) . to . equal ( 0 ) ;
304+ ( ourBoard . sync . sntpActive ) . should . equal ( false ) ;
305+ ( ourBoard . sync . timeOffsetMaster ) . should . equal ( 0 ) ;
306+ ( ourBoard . sync . timeOffsetAvg ) . should . equal ( 0 ) ;
307+ expect ( ourBoard . sync . timeOffsetArray . length ) . to . equal ( 0 ) ;
308+ } ) ;
299309 it ( 'configures impedance array with the correct amount of channels for default' , function ( ) {
300310 ourBoard = new openBCIBoard . OpenBCIBoard ( ) ;
301311 ( ourBoard . impedanceArray . length ) . should . equal ( 8 ) ;
@@ -1085,19 +1095,64 @@ describe('openbci-sdk',function() {
10851095 . catch ( function ( err ) {
10861096 expect ( ourBoard . curParsingMode ) . to . equal ( k . OBCIParsingNormal ) ;
10871097 expect ( ourBoard . sync . curSyncObj ) . to . be . null ;
1098+ expect ( ourBoard . sync . eventEmitter ) . to . be . null ;
10881099 done ( ) ;
10891100 } ) ;
10901101 } ) ;
10911102 it ( "should emit sycned event with valid false" , function ( done ) {
10921103 var timeSetPacketArrived = ourBoard . time ( ) ;
1104+ var expectedTimeSyncOffsetMaster = 72 ;
10931105 ourBoard . curParsingMode = k . OBCIParsingTimeSyncSent ;
10941106 ourBoard . sync . curSyncObj = openBCISample . newSyncObject ( ) ;
1107+ ourBoard . sync . timeOffsetMaster = expectedTimeSyncOffsetMaster ;
10951108 ourBoard . once ( 'synced' , obj => {
10961109 expect ( obj . valid ) . to . be . false ;
1110+ expect ( obj . timeOffsetMaster ) . to . equal ( expectedTimeSyncOffsetMaster ) ;
10971111 done ( ) ;
1098- } )
1112+ } ) ;
10991113 ourBoard . _processPacketTimeSyncSet ( timeSyncSetPacket , timeSetPacketArrived ) ;
11001114 } ) ;
1115+ it ( "should reset when bad raw packet" , function ( done ) {
1116+ var timeSetPacketArrived = ourBoard . time ( ) ;
1117+ var badPacket ;
1118+ if ( k . getVersionNumber ( process . version ) >= 6 ) {
1119+ // from introduced in node version 6.x.x
1120+ badPacket = Buffer . from ( timeSyncSetPacket . slice ( 0 , 30 ) ) ;
1121+ } else {
1122+ badPacket = new Buffer ( timeSyncSetPacket . slice ( 0 , 30 ) ) ;
1123+ }
1124+ ourBoard . sync . curSyncObj = openBCISample . newSyncObject ( ) ;
1125+ ourBoard . _processPacketTimeSyncSet ( badPacket , timeSetPacketArrived )
1126+ . then ( ( ) => {
1127+ done ( "failed to get rejected" ) ;
1128+ } )
1129+ . catch ( function ( err ) {
1130+ expect ( ourBoard . curParsingMode ) . to . equal ( k . OBCIParsingNormal ) ;
1131+ expect ( ourBoard . sync . curSyncObj ) . to . be . null ;
1132+ expect ( ourBoard . sync . eventEmitter ) . to . be . null ;
1133+ done ( ) ;
1134+ } ) ;
1135+ } ) ;
1136+ it ( "should emit bad synced object bad raw packet" , function ( done ) {
1137+ var timeSetPacketArrived = ourBoard . time ( ) ;
1138+ var expectedTimeSyncOffsetMaster = 72 ;
1139+ var badPacket ;
1140+ if ( k . getVersionNumber ( process . version ) >= 6 ) {
1141+ // from introduced in node version 6.x.x
1142+ badPacket = Buffer . from ( timeSyncSetPacket . slice ( 0 , 30 ) ) ;
1143+ } else {
1144+ badPacket = new Buffer ( timeSyncSetPacket . slice ( 0 , 30 ) ) ;
1145+ }
1146+ ourBoard . curParsingMode = k . OBCIParsingTimeSyncSent ;
1147+ ourBoard . sync . curSyncObj = openBCISample . newSyncObject ( ) ;
1148+ ourBoard . sync . timeOffsetMaster = expectedTimeSyncOffsetMaster ;
1149+ ourBoard . once ( 'synced' , obj => {
1150+ expect ( obj . valid ) . to . be . false ;
1151+ expect ( obj . timeOffsetMaster ) . to . equal ( expectedTimeSyncOffsetMaster ) ;
1152+ done ( ) ;
1153+ } ) ;
1154+ ourBoard . _processPacketTimeSyncSet ( badPacket , timeSetPacketArrived ) ;
1155+ } ) ;
11011156 it ( "should calculate round trip time as the difference between time sent and time set packet arrived" , function ( done ) {
11021157 var timeSetPacketArrived = ourBoard . time ( ) ;
11031158 var expectedRoundTripTime = 20 ; //ms
@@ -1554,7 +1609,7 @@ describe('openbci-sdk',function() {
15541609 // Pretend that half of buf1 got sent in the first serial flush
15551610 // and that the last half of it will arrive a lil later
15561611 var splitPoint = 15 ;
1557- if ( process . version > 6 ) {
1612+ if ( k . getVersionNumber ( process . version ) >= 6 ) {
15581613 // from introduced in node version 6.x.x
15591614 ourBoard . buffer = Buffer . from ( buf1 . slice ( 0 , splitPoint ) ) ;
15601615 } else {
@@ -1593,7 +1648,7 @@ describe('openbci-sdk',function() {
15931648
15941649 ourBoard [ "buffer" ] = null ;
15951650 var bufFirstHalf , bufLastHalf ;
1596- if ( process . version > 6 ) {
1651+ if ( k . getVersionNumber ( process . version ) >= 6 ) {
15971652 // from introduced in node version 6.x.x
15981653 bufFirstHalf = Buffer . from ( buf3 . slice ( 0 , splitPoint ) ) ;
15991654 bufLastHalf = Buffer . from ( buf3 . slice ( splitPoint ) ) ;
@@ -2881,7 +2936,7 @@ describe('#daisy', function () {
28812936 } ) ;
28822937} ) ;
28832938
2884- describe ( '#sync ' , function ( ) {
2939+ describe ( '#syncWhileStreaming ' , function ( ) {
28852940 var ourBoard ;
28862941 this . timeout ( 4000 ) ;
28872942 before ( function ( done ) {
@@ -2943,43 +2998,128 @@ describe('#sync', function() {
29432998 it ( 'can sync while streaming' , done => {
29442999 var syncAfterSamples = 50 ;
29453000 var notSynced = true ;
3001+ var syncFunc = obj => {
3002+ ourBoard . removeListener ( 'sample' , samp ) ;
3003+ done ( ) ;
3004+ } ;
29463005 var samp = sample => {
29473006 if ( sample . sampleNumber >= syncAfterSamples && notSynced ) {
29483007 notSynced = false ;
29493008 // Call the first one
2950- ourBoard . syncClocks ( ) . catch ( err => done ) ;
3009+ ourBoard . syncClocks ( )
3010+ . catch ( ( err ) => {
3011+ ourBoard . removeListener ( 'sample' , samp ) ;
3012+ ourBoard . removeListener ( 'synced' , syncFunc ) ;
3013+ done ( ) ;
3014+ } ) ;
29513015 }
29523016 } ;
29533017 ourBoard . on ( 'sample' , samp ) ;
2954- // Attached the emitted
2955- ourBoard . once ( 'synced' , obj => {
2956- console . log ( 'syhnc obj' , obj ) ;
2957- ourBoard . removeListener ( 'sample' , samp ) ;
2958- done ( ) ;
2959- } ) ;
3018+ ourBoard . once ( 'synced' , syncFunc ) ;
29603019 } ) ;
29613020 } ) ;
29623021 describe ( '#syncClocksFull' , function ( ) {
29633022 it ( 'can run a full clock sync' , done => {
29643023 var notSynced = true ;
29653024 var sampleFun = sample => {
2966- console . log ( 'sample' , sample ) ;
2967-
29683025 if ( notSynced ) {
29693026 notSynced = false ;
29703027 // Call the first one
29713028 ourBoard . syncClocksFull ( )
29723029 . then ( syncObj => {
2973- console . log ( syncObj ) ;
29743030 if ( syncObj . valid ) {
3031+ ourBoard . removeListener ( 'sample' , sampleFun ) ;
29753032 done ( ) ;
29763033 } else {
3034+ ourBoard . removeListener ( 'sample' , sampleFun ) ;
29773035 done ( "Not able to sync" ) ;
29783036 }
2979- } ) . catch ( err => done ) ;
3037+ } ) . catch ( ( err ) => {
3038+ ourBoard . removeListener ( 'sample' , sampleFun ) ;
3039+ done ( ) ;
3040+ } ) ;
29803041 }
29813042 } ;
29823043 ourBoard . on ( 'sample' , sampleFun ) ;
29833044 } ) ;
29843045 } ) ;
29853046} ) ;
3047+
3048+ describe ( '#syncErrors' , function ( ) {
3049+ var ourBoard ;
3050+ this . timeout ( 4000 ) ;
3051+ before ( function ( done ) {
3052+ ourBoard = new openBCIBoard . OpenBCIBoard ( {
3053+ verbose :true ,
3054+ simulatorFirmwareVersion : 'v2'
3055+ } ) ;
3056+ var useSim = ( ) => {
3057+ ourBoard . simulatorEnable ( )
3058+ . then ( ( ) => {
3059+ return ourBoard . connect ( k . OBCISimulatorPortName ) ;
3060+ } )
3061+ . then ( ( ) => {
3062+ return ourBoard . softReset ( ) ;
3063+ } )
3064+ . catch ( err => console . log ( err ) ) ;
3065+ } ;
3066+ ourBoard . autoFindOpenBCIBoard ( )
3067+ . then ( portName => {
3068+ return setTimeout ( ( ) => {
3069+ console . log ( 'Issuing connect' ) ;
3070+ ourBoard . connect ( portName ) ;
3071+ } , 500 ) ;
3072+ } )
3073+ . catch ( ( err ) => {
3074+ useSim ( ) ;
3075+ } )
3076+ . then ( ( ) => {
3077+ //console.log('connected');
3078+ } )
3079+ . catch ( err => {
3080+ console . log ( 'Error: ' + err ) ;
3081+ } ) ;
3082+
3083+
3084+ ourBoard . once ( 'ready' , ( ) => {
3085+ done ( ) ;
3086+ } ) ;
3087+ } ) ;
3088+ after ( function ( done ) {
3089+ if ( ourBoard . connected ) {
3090+ ourBoard . disconnect ( ) . then ( ( ) => {
3091+ done ( ) ;
3092+ } ) . catch ( ( ) => done ) ;
3093+ } else {
3094+ done ( ) ;
3095+ }
3096+ } ) ;
3097+ afterEach ( ( ) => {
3098+ this . buffer = null ;
3099+ } ) ;
3100+ describe ( '#syncClocksFull' , function ( ) {
3101+ it ( 'should reject syncClocksFull request because of timeout' , done => {
3102+ var notSynced = true ;
3103+ var sampleFun = sample => {
3104+ if ( notSynced ) {
3105+ notSynced = false ;
3106+ // Call the first one
3107+ ourBoard . syncClocksFull ( )
3108+ . then ( syncObj => {
3109+ done ( "Should not be able to sync" ) ;
3110+ } ) . catch ( ( err ) => {
3111+ ourBoard . removeListener ( 'sample' , sampleFun ) ;
3112+ done ( ) ;
3113+ } ) ;
3114+ ourBoard . streamStop ( ) ;
3115+ }
3116+ } ;
3117+ ourBoard . streamStart ( )
3118+ . catch ( err => {
3119+ ourBoard . removeListener ( 'sample' , sampleFun ) ;
3120+ done ( 'coulnd not start stime sync' )
3121+ } ) ;
3122+ ourBoard . on ( 'sample' , sampleFun ) ;
3123+ } ) ;
3124+ } ) ;
3125+ } ) ;
0 commit comments